Copy link to clipboard
Copied
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_g...
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_g...
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>
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(){w
...
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hi again, I would really appreciate your input in this. Everything seems to be captured by GTM except the purchase event. Below is the code I implemented, could this be a global object issue? Perhaps other third parties code I have are interfering with it in the global object?
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create','UA-9075829-1','auto');
ga('require','ec');
<cfoutput>
<cfif Variables.bTrackReceipt>
<cfloop query="qOfqCart">
ga('ec:addProduct', {
'id': '#courseid#'
,'name': '#JSStringFormat(coursename)#'
,'category': 'Course'
,'brand': 'My Store'
<cfif unitprice EQ unitdiscountprice>
,'price':'#unitprice#'
<cfelse>
,'price':'#unitdiscountprice#'
</cfif>
,'quantity': '#quantity#'
});
</cfloop>
ga('ec:setAction', 'purchase', {
'id': '#JSStringFormat(Session.sPersPayInfo.InvoiceNumber)#'
,'affiliation': 'My Store'
,'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>
<cfif (Application.stSiteInfo.SiteType IS "live")>
<!--- This is the default for tracking the receipt in the javascripts below --->
<cfparam name="Variables.bTrackReceipt" default="0">
<!--- BEGIN for GA4 --->
<!--- file used: js-gtag-ga4 --->
<cfinclude template="/ctolms1/pagelets/js-gtag-ga4.cfm">
<!--- END for GA4 --->
<cfif Request.stPageControls.usegoogleremarketing>
<!--- Using cfinclude so ReceiptXML can be passed --->
<cfinclude template="/ctolms1/pagelets/js-google-remarketing.cfm">
</cfif>
<cfif Request.stPageControls.usegoogleanalytics>
<!--- Using cfinclude so ReceiptXML can be passed --->
<cfinclude template="/ctolms1/pagelets/js-google-analytics.cfm">
</cfif>
<cfif Request.stPageControls.usebinguet>
<!--- Using cfinclude so ReceiptXML can be passed --->
<cfinclude template="/ctolms1/pagelets/js-bing-uet.cfm">
</cfif>
<cfif IsDefined("Request.stPageControls.usecje") AND Request.stPageControls.usecje>
<!--- Using cfinclude so ReceiptXML can be passed --->
<cfinclude template="/ctolms1/pagelets/js-cje.cfm">
</cfif>
Copy link to clipboard
Copied
Hi @user14566 ,
I don't understand what you mean when you say, "Everything seems to be captured by GTM except the purchase event.". Could you please provide more information? The more the better.