How do i install GA4 on website with legecy analytics.js without having to add a second snippet

138 Views Asked by At

I want to install GA4 on a website that already have GA(universal analytics) installed on it using analytics.js . But i don't want to insert an additional script/snippet due to the concern of it adding to page load time. So we just want to have one snippet that would work for both Universal analytics and GA4

Thanks in advance.

1

There are 1 best solutions below

3
BNazaruk On

Thinking that loading more scripts will reduce the speed is flawed. While it may look technically correct, it's too simplistic to be useful. The speed decrease will be rarely measurable from an async load of a small, non-blocking library like gtag.

The industry best practice, however, is to go with GTM or a different tag management system that will load scripts as required. That's exactly the one snippet for them all logic. Again, won't measurably delay the pageload, unless misused.

Finally, if loading a script is such a concern, then why loading analytics.js? Just have it all in the source? Maybe even rewrite the whole thing to send events using the measurement protocol? Same applies to GA4. All these libraries do is abstract the complexity of sending proper network requests to their endpoint.