Skip to main content
Participant
May 17, 2023
Answered

How to replace the analytics.js snippet with the gtag.js snippet in Coldfusion analytics file?

  • May 17, 2023
  • 1 reply
  • 735 views

Hello,

I have to replace the analytics.js snippet with the gtag.js snippet for our site. I've reached out to google several times and I get no response or a response that doesn't address my questions.

After looking through documentations I believe we have to install tag into our code, if so where would I do this?https://developers.google.com/analytics/devguides/collection/gtagjs

Then, it asks to migrate it by following this https://developers.google.com/analytics/devguides/migration/ua/analyticsjs-to-gtagjs#analyticsjs_2_gtagjs

but what exactly would I change on our ColdFusion file (code below) to match this : https://developers.google.com/analytics/devguides/migration/ua/analyticsjs-to-gtagjs#analyticsjs_2_gtagjs

I'm not familiar with this. Please help.

This is our google analytics cfm file:

<cftry>
<cfparam name="Request.sTrackFunnelStep" default="">

<script>
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create','TAD_ID','auto');
    ga('require','ec');

    <cfoutput>
    <cfif Variables.bTrackReceipt>
        <cfloop query="qOfqCart">
            ga('ec:addProduct', {
                    'id': '#courseid#'
                    ,'name': '#JSStringFormat(coursename)#'
                    ,'category': 'Course'
                    ,'brand': 'COMPANY'
                    <cfif unitprice EQ unitdiscountprice>
                        ,'price':'#unitprice#'
                    <cfelse>
                        ,'price':'#unitdiscountprice#'
                    </cfif>
                    ,'quantity': '#quantity#'
            });
        </cfloop>
        
        ga('ec:setAction', 'purchase', {
                'id': '#JSStringFormat(Session.sPersPayInfo.InvoiceNumber)#'
                ,'affiliation': 'COMPANY'
                ,'revenue': '#Variables.nTotalPrice#'
                ,'tax': '#Session.sCartItemTotals.Tax#'
                ,'shipping': '0'
        });
    </cfif>
    
    <cfif Len(Request.sTrackFunnelStep)>
        ga('send','pageview','#Request.sTrackFunnelStep#');
    <cfelse>
        ga('send','pageview',location.pathname);
    </cfif>
    </cfoutput>
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<cfcatch type="Any">
    <cfimport taglib="/ctolms1/mail" prefix="mail">
   <mail:error-mailer>
</cfcatch>
</cftry>
    This topic has been closed for replies.
    Correct answer BKBK

    Google's instructions clearly tell you that:

    "To install the Google tag, copy the following code and paste it immediately after the <head> tag on every page of your site. Replace GA_TRACKING_ID with the ID of the Google Analytics property to which you want to send data. You need only one snippet per page."

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

     

    You say you want that on every CFM page of the application. I see that you're using custom tags. So, you could apply the Google analytics functionality as a custom tag which every page invokes.

     

    Let's name the custom tag's CFM page myGoogleAnalytics.cfm. Let's assume your above ColdFusion code and Javascript code are correct.

     

    Bundle that code, together with all the other Google Analytics code, in the head of the file myGoogleAnalytics.cfm 

    <!--- Save this in the custom-tags directory as the file myGoogleAnalytics.cfm --->
    <cfif thisTag.executionMode EQ 'start'>
    
    	<html>
    	<head>
    	<meta charset="UTF-8">
    	<!--- Put all the Javascript, CFM and CSS code pertaining to Google Analytics here --->
    	</head>
    	<body>
    	
    <cfelse>
    
    	</body>
    	</html>
    
    </cfif>

     

    How do we then ensure that the Google Analytics code runs in the head of every page? One possible solution would be to use the onRequest eventhandler in Application.cfc, as follows:

    <cffunction name="onRequest">
    <cfargument name="targetPage" type="String" required=true>
    	<cf_myGoogleAnalytics>
    		<cfinclude template="#Arguments.targetPage#">
    	</cf_myGoogleAnalytics>
    </cffunction>

     

    1 reply

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    May 24, 2023

    Google's instructions clearly tell you that:

    "To install the Google tag, copy the following code and paste it immediately after the <head> tag on every page of your site. Replace GA_TRACKING_ID with the ID of the Google Analytics property to which you want to send data. You need only one snippet per page."

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

     

    You say you want that on every CFM page of the application. I see that you're using custom tags. So, you could apply the Google analytics functionality as a custom tag which every page invokes.

     

    Let's name the custom tag's CFM page myGoogleAnalytics.cfm. Let's assume your above ColdFusion code and Javascript code are correct.

     

    Bundle that code, together with all the other Google Analytics code, in the head of the file myGoogleAnalytics.cfm 

    <!--- Save this in the custom-tags directory as the file myGoogleAnalytics.cfm --->
    <cfif thisTag.executionMode EQ 'start'>
    
    	<html>
    	<head>
    	<meta charset="UTF-8">
    	<!--- Put all the Javascript, CFM and CSS code pertaining to Google Analytics here --->
    	</head>
    	<body>
    	
    <cfelse>
    
    	</body>
    	</html>
    
    </cfif>

     

    How do we then ensure that the Google Analytics code runs in the head of every page? One possible solution would be to use the onRequest eventhandler in Application.cfc, as follows:

    <cffunction name="onRequest">
    <cfargument name="targetPage" type="String" required=true>
    	<cf_myGoogleAnalytics>
    		<cfinclude template="#Arguments.targetPage#">
    	</cf_myGoogleAnalytics>
    </cffunction>

     

    user14566Author
    Participant
    May 25, 2023

    Thank you so much for confirming. I was able to implement this a few days and I wasn't sure if it was the right thing as google IT instructed to do other things.