How to assign Google Tag Manager Custom Variable to Google Analytics Custom Dimension - google-analytics

I am new to Google Tag Manager and Google Analytics. Having set up basic tags in GTM that are mapped to Google Analytics I want to get improved insights by using HTML meta tag data To that end I have created a custom GTM variable with the following JS:
var metaNames = ["foo", "bar", "blah" ];
var metas = document.getElementsByTagName('meta');
var content = undefined;
for (i = 0; i < metas.length; i++){
if (metaNames.contains(metas[i].getAttribute("property"))){
content = metas[i].getAttribute("content");
}
}
return content;
}
Having read Google documentation and searched the web I cannot figure out how to map the GTM variable to a GA "Custom Definition" or "Custom Metric". The "User Property" / "Event Parameter" field in GA does not appear to correspond to the GTM variable.
I want GA to use the GTM variable to produce analytics based upon the page meta data properties identified above.
Any assistance would be gratefully received.
Thanks and regards,
Andrew

I have figured this out, under GTM Tag configuration the GTM variables need to be mapped to GA4 "Fields to set" or "User Properties". The variable will then be sent as part of the event fired by GTM

Related

How to set `ga_session_id` with google tag manager (GTM)?

I would like to set the ga_session_id that gets reported in google analytics (along with other event parameters) dynamically. With global site tag (gtag.js) I can make the following call to set the ga_session_id:
gtag('config', 'GA_MEASUREMENT_ID', {
session_id: 'my_custom_session_id'
})
How can I accomplish the same thing in GTM?
Set the field session_id in the GA4 settings tag.

Adding a key for "read time" to datalayer for tracking in Google Tag Manager/Analytics

With a WP plugin, I'll be adding "read time" to each article. NOTE: Read time simply shows how long it takes to read the article, not how long a user has read my article.
I want to pull this value into Google Analytics as a custom dimension. I'd like to compare how long people are on the page (avg. time on pg.) against how long it should take to read the whole article (read time).
I understand I need to add a key for "read time" to the data layer, and pull that key into GTM. However, the documentation does not make it clear what key to add to data layer for a value like read time. Or how to create a key for it that Google Analytics will understand if that is possible.
Any possible solutions?
The key can be anything you like. Assuming this info should be available when the article loads, you can push information to the dataLayer from the page code, or from a custom HTML tag in GTM using a window loaded page view trigger.
<script>
// Example of getting the read time value from a
// DOM element with a class "read-time"
var readTime = document.querySelector('.read-time').innerText;
dataLayer.push({
// Give an event name so that we know exactly when
// the data becomes available in the dataLayer.
event: "Article Viewed",
// Add the data
article: {
readTime: readTime,
title: "How to write a blog post",
// other useful article-related properties
}
});
</script>
When you have your dataLayer push working successfully, you can add a dataLayer variable in GTM to make read time available to other tags. The dataLayer key in my code example is article.readTime.
To send this value to GA as a custom dimension:
Add a custom dimension to GA. Set the scope to "hit" and make note of the index for later.
In GTM, add a Universal Analytics tag and set "Track Type" to Event. "Event category" could be "article," "event action" could be "article viewed" and "event label" could be the GTM built-in variable for Page URL or Page Path.
This is important: set "non-interaction hit" to "True."
"Google Analytics Settings" should be your GA settings variable. Check the "Enable overriding settings in this tag" box. Expand "More Settings" and "Custom Dimensions."
Click "Add Custom Dimension" and input the index of the custom dimension that you defined in GA for this purpose. Then, in the value field, put your dataLayer variable for read time.
Add a trigger of type "Event." In the "Event Name" field, put the event name of your dataLayer push ("Article Viewed" in my example). Save the trigger and make sure it's added to the tag.
Save the tag and test it in preview mode.

Issue with Dynamic Remarketing Tag. The Product ID parameter is missing in 99% of the activity

When I go to Tag Google Ads details, I have this issue: "The “Product ID” parameter is missing in 99% of the activity of your Google Ads Tag".
Note, I implemented Dynamic Remarketing Tag via Google Tag Manager. In setting up Tag "Remarketing Google Ads" I inserted:
ID Conversion: XXX-XXX-XXX;
Custom Parameters: Use Data Layer {{google_tag_params}};
Trigger: All pages.
Following you can see source code html for a product page that contains ecomm_prodid, ecomm_pagetype, ecomm_totalvalue:
<!-- BEGIN: Google TagManager Remaketing -->
<script>
var dataLayer = window.dataLayer || [];
dataLayer.push({
'event':'remarketingTriggered',
'google_tag_params': {
'ecomm_prodid': '721',
'ecomm_pname': 'MAGLIONE GIROCOLLO IN LANA',
'ecomm_pcat': 'home',
'ecomm_pagetype': 'product',
'ecomm_totalvalue': '20.00'
}
});
</script>
<!-- END: Google TagManager Remaketing -->
So, I created in GTM a variable called google_tag_params with:
Data Layer Variable Name: google_tag_params;
Data Layer Version: Version 2.
As seen before, I used this variable in Remarketing Google Ads Tag, trying to capture at least: ecomm_prodid, ecomm_pagetype, ecomm_totalvalue needed to Display Remarketing.
When I use GTM in preview mode I have this:
Send dynamic remarketing event data: false
Variabile di livello dati:
{ecomm_prodid: '721',
ecomm_pname: 'MAGLIONE GIROCOLLO IN LANA',
ecomm_pcat: 'home',
ecomm_pagetype: 'product',
ecomm_totalvalue: '20.00'}
ID conversione: 'XXX-XXX-XXX'
Parametri personalizzati: 'DATA_LAYER'
Problem is issue "The “Product ID” parameter is missing in 99% of the activity of your Google Ads Tag" is still there.
I tried also check via Tag Assistant with Product Feed ID XXX-XXX-XXX, but I got this alert: Some products are not valid.
When I check on Metadata request in Tag Assistant I got this "event:gtag.config" and not parameters.
I searched for days on web, but did not find any solution.
Could you please assist me?
Thanks,
Francesco
Try the following:
Create a dataLayer variable for each of your dynamic remarketing variable.
google_tag_params.ecomm_prodid
google_tag_params.ecomm_pname
google_tag_params.ecomm_pcat
google_tag_params.ecomm_pagetype
google_tag_params.ecomm_totalvalue
Use the following custom event as you have in the dataLayer event field.
Use the manually specify on your AdWords tag:
Verify the implementation by checking in your browser network tab, filtering by the keyword random. You should see the dynamic remarketing request along with the key/value you've provided.

GA/GTM Custom Dimension Error (Undefined)

kindly ask you to help me with custom dimensions, as i'm newbie for GA/GTM and did everything per manual.
1) So i created 3 new dimensions for ecommerce in GA:
here is dimensions from GA with indexes
2) I've created snipped in GA and added it into head section of my site:
<script> gtag('event', 'page_view', {
'send_to': 'AW-XXXXXXX',
'ecomm_pagetype': 'page_type',
'ecomm_prodid': 'product_ids',
'ecomm_totalvalue': 'total_value',
'user_id': ‘userId’ }); </script>
along with global site tag.
3) Finally i've created new variables in GTM:
for example for page_type
and in Tag itself.
but i constantly getting error on debug:
{index: '1', dimension:undefined}
screenshot is here
will really appreciate any help!
p.s. sorry for links with images instead embedding, i have not enough reputation here yet.
You clearly are mistaken the gtag which stands for global site tag with the tag manager code.
Instead of using gtag use dataLayer.push, this is the method to push information to Google Tag Manager.
An example:
dataLayer.push({
'ecomm_pagetype' : Variable1,
'ecomm_prodid' : Variable2,
'ecomm_totalvalue' : Variable3
});
Here you have the docs for further reference: docs
Remember to place this tag under the Tag Manager snippet. or remember to initialize an empty dataLayer as the doc states.
Code on page
On your page, before your GTM container snippet add the following (I've used sample values for type, id and value), your code should look like below:
<script>
dataLayer = [{
'ecomm_pagetype': 'MyPageType',
'ecomm_prodid': 'EXAMPLE0000001',
'ecomm_totalvalue': '4000',
}];
</script>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXX');</script>
<!-- End Google Tag Manager -->
GTM - Data Layer Variables
In GTM, create 3 data layer variables for "ecomm_pagetype", "ecomm_prodid", "ecomm_totalvalue" like so:
Type in the key(name) that's in the datalyer object, you can set a default value if you like, otherwise, it will be "undefined" for pages without this variable defined.
Next create a "Google Analytics Settings" variable, this is to simply GA tag configuration, we will link the custom dimensions here.
Put in your own GA property ID, under "More Settings" expand to "Custom Dimensions", click on "ADD CUSTOM DIMENSION". Put in "1" for to reference the index for "ecomm_pagetype" CD in GA. For value, use the variable we created earlier. Do this for all 3 CDs.
Now your Variables page should look something like this:
GTM - Tags, adding CDs to GA tag
Now for your tag. You should have a Google Analytics tag configured in GTM. Looks like you know how to do that, to add the custom dimensions by using the new "My GA Variable" under "Google Analytics Settings". Don't forget to have this trigger on "All pages" or pick your own trigger as you see fit.

Custom Dimension value alway showing up as not set

I am trying to create custom dimension using GTM, dataLayer and GoogleAnalytics.
In GTM, created a user defined variable.
UserVariable
I have something like this in my html page
<script>
function addCategory(){
dataLayer.push({
'category' : 'drama'
}
);
}
</script>
</head>
<body onload="addCategory()">
In Google analytics created custom dimension
Custom Dimension
In the GTM in tags section, associated the dimension that was created with the user defined variable.
Index - 2
Dimension Value - {{category}}
In the Preview and Debug mode i can see that value in the Variables section.
category Data Layer Variable string 'drama'
But in Google Analytics i always seeing that value as (not set).
What have i done wrong
Any custom definition should be sent with an existing hit. Can you confirm that a pageview or an event is fired after you defined the variable? Check this answer that is describing how to send a custom dimension with GTM. In step 3 it is described how to use the Universal Analytics tag to send the custom dimension to GA. I suspect that your custom dimension is never included in an analytics hit that is why you see (not set).
Also, I would advise against using variable names such as 'category' in GA because it is resembling a variable 'used' by enhanced ecommerce.

Resources