Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including GA4 within a JavaScript file

UA did not require additional scripts for us. I’ve just been tasked with including the new GA4 scripts. The site has an include file(analytics.js) spattered across some 900+ pages with various predictability(all hardcoded)

Analytics.js(old ua script)

I was curious to know if it’s possible to include the new GA4 required scripts(code below) within the js file as that file is the only appropriate place without making updates to 900 pages.

<!-- Google tag (gtag.js) -->
  <script async src="https://www.googletagmanager.com/gtag/js?id=TAG_ID"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', 'TAG_ID');
  </script>
like image 402
user3135730 Avatar asked Jul 02 '26 09:07

user3135730


1 Answers

oof, that's a lot of pages, not sure if that will work but I would recommend looking into Google Tag Manager if you haven't already.

It will take care of the GA4 script for you but you'll have to go through the pain of updating 900+ pages with the GTM script.

It will be worth it in the long run. GTM allows you to not only add GA4 with other functionality but manage other scripts/tags as well.

like image 133
Arty.Simon Avatar answered Jul 05 '26 00:07

Arty.Simon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!