Learn business growth with Google Analytics 4 Forums Google Analytics 4 How to Simultaneously Utilize Google Analytics 4 and Universal Analytics

  • How to Simultaneously Utilize Google Analytics 4 and Universal Analytics

    Posted by Benjamin on 5 April 2023 at 7:04 pm

    Hey there! Looking for a little help with some code – I’m trying to run Universal Analytics (UA) and Google Analytics 4 (GA4) simultaneously on the same webpage. Used the script below to set things rolling. Is this the right route? Or have I goofed up somewhere? Here’s the code in question, any insight would be much appreciated.

    <script async src="https://www.googletagmanager.com/gtag/js?id=G-xxxxxxx"></script>
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxx-x"></script>
    <script>
            window.dataLayer = window.dataLayer || [];
    
            function gtag() {
                dataLayer.push(arguments);
            }
    
            gtag('js', new Date());
    
            gtag('config', 'G-xxxxxxx', {'debug_mode': true});
            gtag('config', 'UA-xxxxxxxx-x', {'debug_mode': true});
    
    </script>
    
    John replied 11 months, 1 week ago 3 Members · 2 Replies
  • 2 Replies
  • Harper

    Member
    21 May 2023 at 6:07 pm

    You’re on the right track and the code looks almost correct, but there’s a slight mistake. You don’t need two separate script tags for the Google Tag Manager, you can load multiple tags through a single script. You should remove one of them, ideally the UA one. Instead, combine the id references in the gtag.js URL and gtag(‘config’) commands. Like this:

    <script async src="https://www.googletagmanager.com/gtag/js?id=G-xxxxxxx"></script>
    <script>
            window.dataLayer = window.dataLayer || [];
    
            function gtag() {
                dataLayer.push(arguments);
            }
    
            gtag('js', new Date());
    
            gtag('config', 'G-xxxxxxx', {'debug_mode': true});
            gtag('config', 'UA-xxxxxxxx-x', {'debug_mode': true});
    
    </script>
    

    Now both GA4 and UA tracking will be linked, with the gtag.js loading only once. Remember to replace ‘G-xxxxxxx’ with your GA4 id and ‘UA-xxxxxxxx-x’ with your UA id. This will allow you to have both UA and GA4 tracking on your webpage at the same time.

  • John

    Member
    8 July 2023 at 4:38 am

    The idea of running Universal Analytics (UA) and Google Analytics 4 (GA4) simultaneously on the same webpage is a common approach when transitioning from UA to GA4, as it allows you to have both versions running at the same time. The script you presented does reflect this approach. However, you don’t need to include the ‘gtag.js’ script twice for both UA and GA4. You only need to include it once, then specify both tracking IDs (GA4 and UA) in the ‘config’ calls within the script. ‘Debug_mode’ is also an optional parameter. If it’s set as true, it will produce verbose debug information to the console; remove it or set as false in a live environment.

Log in to reply.