1
votes

I recently transitioned my company's websites to Google Tag Manager. Due to the fact that our contact forms don't have unique confirmation pages with unique URLs, we use virtual pageviews to track conversions. The form is submitted, and the page reloads with new content and our conversion tracking codes.

Since migrating to GTM, all of our virtual pageviews stopped working. Now when I submit a form on our site, I get this in the console log:

ReferenceError: ga is not defined
  ga('send', 'pageview', '/funnel_G1/premium1.html');

Before, we had Universal Analytics loading directly on the page. Now we are loading Universal Analytics through GTM. That is all that has changed and I can't figure out why our virtual pageview scripts no longer work.

This is script that fires on a form completion:

<script type="text/javascript">
$(document).ready(function () {
  ga('send', 'pageview', '/funnel_G1/premium1.html');
});
</script>
2

2 Answers

2
votes

When you moved to GTM, you likely also correctly removed all on-page GA code, which includes the standard GA snippet and also the

ga('create', 'UA-.....');
ga('send','pageview');

code. Removing the GA snippet also removes the creation of the "ga" object, which is why you are getting the error you see. With GTM, you will now need to send in your events and pageviews with tags. So what you could try to do is to push events to the dataLayer that help to trigger tags that fire your virtual pageview. In your specific case, you could do something like this when the form is submitted:

dataLayer.push({
   'event': 'form complete',
   'vpv': '/funnel_G1/premium1.html'
})

and then in GTM, you would need to create pageview tag that is triggered by the event 'form complete', and that also alters the page value to the value of the 'vpv' dataLayer key (ie. /funnel_G1/premium1.html).

1
votes

The problem is that, GTM loads ga() function asynchronously. If you call ga() before it's loaded, you will run in to this error.

Solution is to somehow wait for ga() to be defined, or use dataLayer.push()


Edit

The solution we ended up using - we just did not include Analytics in the GA, but included them separately in source. GTM would handle other script icnludes except GA. This solution atleast did not break ga() usages throughout our project.