Passing variable with changeable value to GTM - google-tag-manager

i would like to track user behaviour on the website depending on they are logged in or not.
I’d like to create my own variable in JavaScript, kind of userState with different values, let’s say, logged and guest, and I want to pass it so dataLayer.
How to do it? Can I do something like this
dataLayer.push({‘userType’: userState})
?

Basically this will work. However, GTM will only update its internal state when a GTM event is registered. When you you dataLayer.push({‘userType’: userState}) the new value for userType will not be automatically available. It needs to be followed by an event, either one that is set up via a GTM auto event variable (such as gtm.click when you use a click trigger), or a custom event:
dataLayer.push({
"userType": userState,
"event":"updateUserState"
});
(also note that you use backticks in your example, which will give an error - you must use straight or double quotes).
A custom event is literally the key "event" with a custom value pushed to the dataLayer. GTM overwrites the push method of the dataLayer array to scan new pushes for the event keyword, and then updates the available variables within GTM.

Related

Google Tag Manager - dynamically send custom events with their params to Google analytics

I'm using Google Tag Manager (aka: GTM) to send events to my Google Analytics 4 account, some of them are custom that developers send thru the code like that:
dataLayer.push({
event: 'CA:some-event',
a: 1, // custom param
b: 2 // custom param
})
I defined a Trigger in GTM to catch all events that start with "CA:*" (as regex) and then i defined a new Tag: "Google Analytics: GA4 Event" to be invoked when the trigger fired and send the event to GA4.
I'm getting the events successfully to my GA4 account but the problem is that all params, a and b in my example, are not sent as they are not mapped to be sent from GTM layer within the GA event.
I can define Variables in GTM specifically for them but it's not dynamic and it means that every time a developer needs to send a new custom event he'll need to go to the GTM account (and usually they don't have access to it), add the variables and it makes the whole solution semi-automatic and not dynamic enough.
I hope it's clear.
Any suggestions here?
Unfortunately there is no easy way to manage GTM as the way you want.
Hope they can allow us to use just JavaScript object to send as event parameter. (They only allow Ecommerce DataLayer at this time)
But looks like your developer can decide the event parameter themselves. So the option here is if they don't have access to GTM. They can just tell the one who is handling the GTM that their new implementation. So the GTM guy can modify based on it.
If your scenario is the changing is a lot and it really need to be automatic. Then GTM API might be the way.
(But it also take some times for developer to integrate with it)
I used to manage more than 10+ properties and more than 30+ new events needs to add in a week.
So I choose Google Sheet(with Google App Script) to manage the GTM more easily.
The point here is there are still some ways to make it more automatic. Just need to leverage is it worth it do develop.
You can use the data layer variables in Google Tag Manager to dynamically send the custom parameters to Google Analytics 4.
Here's how you can set it up:
Create a Data Layer Variable in GTM to capture the custom parameters. To do this, go to Variables > New > Data Layer Variable. Name the variable and enter the parameter name in the Data Layer Variable Name field (e.g. "a" or "b").
Add the Data Layer Variables as fields in your Google Analytics 4 Event tag. To do this, go to your Google Analytics 4 Event tag and select "More Settings" > "Fields to Set". Enter the name of the Data Layer Variable you created in step 1 (e.g. "a" or "b") and the value {{a}} or {{b}} respectively in the value field.
Publish your changes in GTM.
Now, every time a custom event is pushed to the data layer, the associated custom parameters will be automatically sent to Google Analytics 4 as well. This allows you to dynamically send custom parameters without having to manually update the Google Analytics 4 Event tag each time a new custom event is added.
Note: Make sure that the custom parameters are consistent across all events, otherwise you may end up with mismatched data in Google Analytics 4.
If you feel worried about DataLayer then you can use JavaScript for scrapping Data. Anyways, Maybe It looks hard, If yes then you can connect to me for getting A-Z help. I'm GTM, GA4, and Conversion Tracking expert : Fb Profile
Thanks.
i ended with the following solution:
I changed the way I'm sending the event to be:
dataLayer.push({
event: 'CA:some-event',
data: {
a: 1, // custom param
b: 2 // custom param
}
})
set a variable in GTM called "Custom Event Data" based on the data property of the event
set the GA4 event tag to send it to GA4
set a new Custom Dimension in GA4 called "Custom Event Data" based on the data property
The limitation is that I cannot use a and b directly in GA4 as a native fields but at least I can see the variation of them in the GA4 reports

How to Trigger a GTM event based on Cookie is accepted or not

There is a piece of script in GTM ( as tag ) that needs to trigger only when user accepts a particular cookie. Problem is - I have added a script belonging to the particular cookie category via GTM. When a user wants to accept only analytics ( GTM loads as expected ), the script I have added via GTM which belongs to another cookie category ( not analytics ) also fires ( Ideally it shouldn't cos only analytics is accepted by the user )
. Is there a way to trigger GTM event only when cookie is accepted.
If I understood correctly, you're looking to load the functional script ONLY if they accept AND functional cookie is 'yes'
In GTM, there are "Variables" that can keep track of 1st party cookies. So in y our case you would want to:
Create a variable to track the functional cookie value
create the trigger based on clicking on "accept" and it would just be conditional based on the value being "yes" (you will need to create your custom functional script tag beforehand)
Add the trigger to the functional tag

Push additional custom variable in transactional event at Google Tag Manager

We have set up a transaction custom event in google tag manager with the name 'purchase' (as in documentation) and we are pushing with the following code correctly:
(<any>window).dataLayer.push({
event: 'purchase',
transactionId: id,
transactionTotal: totalPrice,
transactionTax: 0.0,
transactionShipping: 0.0,
transactionProducts: products,
});
where products is another array-object.
This works fine but we wanted to add another one variable(transactionCustomVariable) in the above json like the following:
(<any>window).dataLayer.push({
event: 'purchase',
transactionCustomVariable: 0,
transactionId: id,
transactionTotal: totalPrice,
transactionTax: 0.0,
transactionShipping: 0.0,
transactionProducts: products,
});
and it seems we cannot pass this value to google tag manager.
Is there a specific naming convention on new fields for a Transactional Event.
We have created the variable name. Is there anything we are missing there?
Thanks in advance
GA native tags in GTM can handle "transparently" values from the dataLayer when it follows a given naming convention designed to collect Enhanced eCommerce tracking details
See
https://developers.google.com/tag-manager/enhanced-ecommerce#data-layer
When you want to push additional values to GA, you can decide to reserve slots for data storage in GA, called Custom Dimension or Custom Metric.
These slots can have different scope: User, Session, Hit, Product and are defined by an Index (in the admin interface)
When you need to populate Product scope values, you have a naming convention to follow for automatic handling from the eCommerce dataLayer to the GA hit, since it is specific to Enhanced eCommerce
https://developers.google.com/tag-manager/enhanced-ecommerce#custom-dimensions
For every other scope of Custom Dimension / Metric, in GTM you need to explicit the mapping between the dataLayer Attribute => a GTM Variable (possibly with a different name) => the Custom Dimension / Metric Index defined in the GA admin interface.
I recommend you use a GA Settings Variable to define that mapping only once and use it for every hit.
You may also use a dataLayer attribute in other tags than GA, you'll be able to use the same GTM Variable to access its value and map to parameters of those tags.
I recommend that you first audit how the dataLayer gets updated when you push data, with the help of the GTM debug mode.
Next, you verify that the tag using that value is consuming it properly and pushing it forward, if applicable.
I suggest that you use https://chrome.google.com/webstore/detail/adswerve-datalayer-inspec/kmcbdogdandhihllalknlcjfpdjcleom that allows auditing of both dataLayer and GA hits

Is it possible to add a datalayer variable to all events at once?

In google tag manager, is there any way to insert variables in the datalayer for all datalayer events? For example have a 'userType' variable that is present in all events.
Do I have to manually add it to all my gtm events in dataLayer.push() or is there a handy work-around?
You don't need to do it, the dataLayer is designed by default to persist variables across events (except for the ecommerce dataLayer so there are no duplicate transactions):
https://www.simoahava.com/gtm-tips/remember-to-flush-unused-data-layer-variables/
So if you push an event with a foo variable, that foo variable will also be available during later events.
If you want a variable not to persist, set it to undefined:
dataLayer.push({
'event' : 'my_event',
'foo' : undefined
});

Event Tracking, Virtual Pageview AND Custom Variables

I am wondering if I can pass an Event, Virtual pageview and a a custom variable at the same time.
More specific...a user fills out a form. I need that lead to be recorded as an event in order for it to be standard across all platforms. I need a Virtual pageview in order to provided funnel analysis. And, I need Custom Variables to help segment my leads.
Any ideas on what this code may look like using Universal Analytics?....or if not, any suggestions with what it would like using ga.js.
Is this possible?
You bet. Put this onclick on your input type = submit:
onclick="_gaq.push(['_trackEvent', 'Form', 'Submit'],['_setCustomVar', 0, 'Name', 'Value', 1], ['_trackPageview', '/thankyoupage']);"
Essentially, you are doing One Push, Multiple Commands - https://developers.google.com/analytics/devguides/collection/gajs/#MultipleCommands. Just remember that a Custom Variable must always get sent with either a pageview or event.

Resources