user1721135
user1721135

Reputation: 7092

Execute Google Analytics Functions in addition to Google Tag Manager

When using the Google Tag Manager, is it possible to track some things the old way in addition to using the GTM?

For example, I use GTM in order to fire a page view.

However, I want to fire another page view, when a user clicks a button, also known as a virtual page view.

The button in question doesn't have an ID and I don't trust the othet agency, which handles these buttons to consistently keep the same IDs for these elements. So I would rather have them be responsible for these types of page views.

The code for the virtual page view would look something like that:

ga('send', {
  hitType: 'pageview',
  page: 'button2'
});

Since the tracker is already initialized by GTM, I would only have this code outside GTM.

Would this work if all other google analytics related things run over gtm and where should I put this code in this case? Somewhere after the GTM code on the page I'd imagine?

Upvotes: 0

Views: 306

Answers (1)

kgrg
kgrg

Reputation: 1633

Google Tag Manager (GTM) by default uses a random name for each tracker, generated for each Universal Analytics tag. There is a possibility to use fixed name for trackers, which is highly discouraged. This means, that you might have difficulties to identify the proper tracker to use, when sending your additional pageview data.

There are however other methods to send virtual pageviews using GTM, where you can benefit from your existing Analytics settings, defined in Google Tag Manager. (Preferably by using Google Analyitcs Settings variable.)

As far as I understand, you have control over the code, to run some JavaScript on the relevant click event. So instead of directly invoking the ga object, you can send the desired data to GTM, with a call like this:

dataLayer.push({
  event : 'virtualPageView',
  virtualPagePath : 'button2'
});

Obviously, there are a couple of things you need to set up in GTM, which will be able to act on this event, and send the pageview to Google Analytics.

  1. Create a variable that points to virtualPagePath dataLayer variable, so the newly pushed value could be reused
  2. Create a custom event trigger, that can be used with one or more tags. The event name should match your given event name, virtualPageView in my example.
  3. You need an Universal Analytics tag, which will send the pageview. This tag should be fired by your new custom event trigger, and should have an extra setting compared to your regular pageview tag. Namely, page variable within the Fields to set block should point to the newly created dataLayer variable, that contains your virtual page path.

This way, Google Tag Manager will take care of creating the tracker for you, sending the hit to Google Analytics, and using the virtual page path variable provided by you, instead of the URL in the browser address bar.

Upvotes: 1

Related Questions