Skip to main content
Participant
June 26, 2023
Question

Why is my "purchase" event is not being picked up by GTM?

  • June 26, 2023
  • 1 reply
  • 190 views

I've read the documentation and I'm currently attempting to migrate to gtag 4, 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>
    This topic has been closed for replies.

    1 reply

    Community Expert
    June 27, 2023

    I'd be interested in seeing what actually got generated (in other words, view source for the first snippet) and what happens in the browser when you interact with it (in other words, look at developer tools). That's a high-level response I just gave you, not much detail, but it's where I'd start.

     

    Dave Watts, Eidolon LLC

    Dave Watts, Eidolon LLC
    Community Expert
    June 27, 2023

    Also, I forgot to add, see if what's in the third argument of the "purchase" function is valid JSON. If you have any single quotes in your CF variables, they'll break the single quotes of your JSON string.

     

    Dave Watts, Eidolon LLC

    Dave Watts, Eidolon LLC