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

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

Related

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

Passing variable with changeable value to GTM

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.

How do I create an Out of office type Calendar Event via the REST API?

Google Calendar has a new event type called "Out of office" that automatically will decline any events it is placed over and others that come in during the scheduled time of the event.
I inspected one of these event objects via the API and briefly checked the Google Calendar API documentation to see if there was a way to create these programmatically. I work on a system that schedules events on people's calendars when they are going to be out of the office as all day events to provide visibility to the rest of the staff. I would like to change these to be true Out of office event types if its possible via the API.
The short answer: It does not work currently (as of 08/2022)
The Google Calendar API currently exposes the eventType property as read-only. It will be discarded when sent in an insert payload.
eventType [string]: Specific type of the event. Read-only.
Possible values are:
"default" - A regular event or not further specified.
"outOfOffice" - An out-of-office event.
As mentioned by #ercliou in the comments, the ticket can be tracked here: issuetracker.google.com/issues/112063903
The only thing that seemingly marks an event as out of office is a private extended property on the event resource:
"extendedProperties": {
"private": {
"everyoneDeclinedDismissed": "-1"
}
}
Looking at https://developers.google.com/calendar/v3/reference/events/insert the attribute extendedProperties.private is writeable so you should be able to set it there.

Is there an equivalent of the Google analytics custom variables "page" scope for Google analytics dimensions

I am trying to upgrade a site's Google Analytics (GA) from the previous ga.js API to the newer analytics.js API. I have an issue with the conversion of Custom Variables into Custom Dimensions.
Previously, custom variables had their scope set to "page". However, when I try to create a custom dimension there is no "page" scope. Is there a Dimension scope value equivalent to the "page" scope used in custom variables from the old api.
ga.js api custom variable scope can have one of these values:
Visitor
Session
Page
In the new analytics.js api the scope value for a dimension can be:
Product
Hit
Session
User
Hit level corresponds to page level, it is just a more generic term - each pageview is a hit, but not every hit is a pageview (you can append dimensions to events etc. wich are also hits). So simply sent a hit level dimension along with a pageview (i.e. in the configuration object of the pageview, not via a "set" call).

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