GTM - fire event to Google Analytics for each element of array - google-analytics

I have a question regarding targeting multiple array members via GTM. I push an array to the dataLayer. Is there a way to loop over the array and then push an event for each element of the array to Google Analytics?

Related

Create Custom dimension based on Event in GTM

We already set up and event for an interaction. However, I wish this event to be captured as session level in Google Analytics. I'm wondering how I can create utilise these event variables in GTM to create a custom dimension.
You have to create before the custom dimension in Google Analytics, then via GTM you can send the information value in that custom dimension index with Google Analytics tag.

can the dataLayer be defined above the gtm script and then data be pushed to dataLayer below the gtm script?

in google documentation for dataLayer it says
If you put the Google Tag Manager container snippet above the
dataLayer array, Google Tag Manager may malfunction, and will not be
able to read the variables defined in the array.
my question is: is it possible to declare the variable(dataLayer) above the GTM container and then push events to it (dataLayer) below the GTM?
Yes. That's not only possible, that's what GTM itself does (if you have a dataLayer variable declared, GTM pushes its gtm.js, gtm.dom and gtm.load event to the existing dataLayer (if this is undefined it declares the variable itself).
One thing you need to remember is that you need to push a GTM event - literally, a key/value pair where the key is "event". The GTM code amends the native push-Method of the datalayer to scan for the "event" keyword, and if the keyword is found it updates its internal variables (i.e. you cannot access the new variables before a GTM event has been pushed).
E.g. if you wanted to have a dataLayer variable "foo" with the value of "bar" you'd need to do
dataLayer.push({
"foo":"bar",
"event":"myCustomEvent"
});
You can then access the foo variable and retrieve its value. You can also do a custom event type trigger, that fires as soon as the event "myCustomEvent" is pushed (you do not have to, though, the "bar" value will be retained until the page is unloaded, you push another value to the "foo" key or you reset the dataLayer).
Some triggers types like click, submit and visibility provide their own events.
The other thing to keep in mind is that you must not declare the dataLayer variable a second time after the GTM code has loaded, else you remove the changes GTM has made to the dataLayer and GTM breaks.
But in short, yes, that's totally who it is supposed to work, you can push values after the GTM code.

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?
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.
Create a variable that points to virtualPagePath dataLayer variable, so the newly pushed value could be reused
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.
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.

Push data to dataLayer and send it to Google Analytics at the same time in Google Tag Manager

I have a complicated issue with GTM, dataLayer and GA.
My setup is:
GA tag is triggered in PageView with Enhanced Ecommerce enabled by using dataLayer
A Custom HTML tag which pushes an item to dataLayer, and it's configured to run just before the GA tag (by using tag sequencing)
Problem is; GA is not including dataLayer changes made by custom Tag even if it's fired after Custom tag.
I noticed, dataLayer.push(...) creates a new GTM event named message.
How can I handle this, GA pageview tag must have the final dataLayer.
tHanks
The datalayer is not re-evaluated in a tag sequence. However you can manipulate the dataLayer directly.
First you need to make sure that the built-in "Container ID" variable is active (or you pass in the ID manually).
Then you get a reference to your GTM instance and call a dataLayer.set with a key and value:
var gtm = google_tag_manager[{{Container ID}}];
gtm.dataLayer.set('someKey',"someValue")
Unlike a dataLayer.push this will be available in the next step in the sequence.

Google Tag Manager - Enhanced Ecommerce and ajax

In my product page detail, i have a crosselling section which loads a product list when user scroll down. I can trigger a custom event when products in this section are loads (like productsDowloaded).
How can i send product impressions with google tag manager ?
Implement the data layer following these specifications to track product impressions. If you are sending the data asynchronously, push an event to the data layer (eg: event: 'crossSellReady') and fire your custom event GA tag with a rule based on event = crossSellReady. In the custom event tag, make sure to enable enhanced eCommerce tracking, and the tag should automatically pick up the product impression data contained in the data layer. Let me know if you encounter any issue.
When the page is first loaded the first batch of cross sell products are pushed to the dataLayer as impressions and a GA pageview is sent. The GA pageview is a simple GA tag that is fired after the page is loaded.
After that, when the cross sell products are loaded asynchronously, i.e. whenever the user scrolls down, only those products are pushed to the dataLayer and a custom dataLayer event is fired in the success handler of the ajax request. For example:
// Inside Ajax success handler...
dataLayer.push({
'event': 'Custom'
'eventCategory': 'ecommerce',
'eventAction': 'ajax-load'
'eventLabel': 'cross sell'
});
You create a separate generic GA tag that sends events and is fired when the Custom event is pushed to the dataLayer. Inside the generic GA event tag you set all the of the following macros which are of type dataLayer variable:
{{event category}} -> eventCategory
{{event action}} -> eventAction
{{event label}} -> eventLabel
You can also add a nonInteraction macro in the generic GA event tag as well. Your use case, in my opinion, is considered a user interaction (user scrolled down) so the nonInteraction macro should not be set.
If you are using product lists, be careful of the positions of the cross sell items. Whenever those products are asynchronously loaded, you should note down their positions so you can track their positions in the product list tab of the GA dashboard correctly.
P.S. The generic GA event tag has multiple benefits, you can find more information about what I'm saying by reading the relevant article in Simo Ahava's blog.

Resources