Show custom dimensions values in google analytics - google-analytics

My goal is to have user email address in google analytics reports so I can build custom reports for client.
For this, I added a custom dimensions "cdUserEmailAddress" under admin --> custom definitions --> custom dimensions
I set it's scope to session.
I set the following code in my page to add a test value but it does not add the value anywhere that I can find on google analytics dashboard.
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
dataLayer.push({
'cdUserEmailAddress': 'test#test.com',
'event': 'sessionUserLoggedIn'
});
gtag('config', 'UA-xxxx-x');
ga('create', 'UA-xxxx-x', 'auto');
ga('set', 'cdUserEmailAddress', 'test#test.com');
ga('send', 'pageview');
console.log('working 2');
</script>
This did not work.
I then went to Google TagManager (since one of the articles I read suggested I need to set that up too) and added a user data element there.
Pasted the auto generated code shown below but that did not help either.
<!-- 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-xxx');</script>
<!-- End Google Tag Manager -->
What am I missing?
I looked everywhere for this test#test.com value ...by creating a custom report, under Behavior -> Site content --> AllPages...
I find it no where.
Please help
Thanks

First of all, you should be aware, that sending email (or any other personally identifiable information) to Google Analytics is violating the terms of service.
Newertheless, if you decide to go for any other custom data, the following should be changed in your code. Custom dimensions and metrics are not referenced by their names, but by their ID, which can be looked up in the administration panel of Google Analytics. For further details please check this detailed guide.
So this part:
ga('set', 'cdUserEmailAddress', 'test#test.com');
Becomes:
ga('set', 'dimension1', 'your non-pii data'); //update the number according to your settings
Also, please note, that generally it's not suggested to send data to the same Analytics property directly from ga() calls and GTM, as you need to maintain your tracking settings in parallel, and you can easily send pageview twice, which is usually not desired.
If you decide to go for GTM, then you need to set up a dataLayer variable, that references your key used in the dataLayer (cdUserEmailAddress in your present case), and you need to use this variable in the Universal Analytics settings, where you can set up custom dimension values, also by referring their IDs.

Related

How do I exclude URL query parameters in the new Google Analytics 4 (not Universal Analytics)?

In the dashboard of the new GA4, if I go to Engagement, Page and Screens, then press Page path and screen class I see repeated urls with query parameters with "?"
example.com/?utm_source=...
example.com/
I would like to ask how to unify the page paths
Query parameter removal with App + Web is a bit different because of the automatic pageview tracking. If you need to remove parameters from your page paths, you can use the page_location field in your Configuration tag in GTM. Keep in mind that there are no filters or view settings to strip query parameters in the Google Analytics interface as we saw for Universal Analytics.
i.e. If you want to remove all query parameters from your page path, we'll use the same method but a different Custom Javascript Variable.
In a new Custom Javascript variable paste the following:
function() {
return document.location.hostname + document.location.pathname;
}
https://www.bounteous.com/insights/2020/05/15/excluding-url-query-parameters-google-analytics/
You can use the config object in your measurement code to override the location string that is reported. You should see a line of code that goes like gtag('config', '...your id...') or gtag('config', '...your id...', { /* some config */ }). You need to either add the 3rd config parameter, or if it is already there you need to add a new attribute into it, so your code should look something like this after all:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=...your id..."></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '...your id...', {
'page_location': location.protocol + '//' + location.host + location.pathname,
/* other options if you have anything else */
});
</script>
Please note that this will remove all URL parameters so everything after the ?. If you only need to remove some, you need to implement a bit more logic and set page_location to the modified URL with only the unwanted parameters removed.

How to not send data to GA when debugging locally?

As the documentation says:
There is a debugging version of Google Analytics that will print extra info to the console for debugging purpouses. However, this version will send data to GA even when it is only for debugging.
According to this documentation (that is a bit outdated), we need to add this code to our Google Analytics code to avoid sending hits to GA:
if (location.hostname == 'localhost') {
ga('set', 'sendHitTask', null);
}
However, I'm using a newer version of GA that uses gtag in the tracking code, So I've change the ga function to gtag:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-134628373-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-134628373-1');
if (location.hostname == 'localhost') {
gtag('set', 'sendHitTask', null);
}
</script>
Just for clarification:
if (location.hostname == 'localhost') {
ga('set', 'sendHitTask', null);
}
to:
if (location.hostname == 'localhost') {
gtag('set', 'sendHitTask', null);
}
Is this the correct approache? I don't want to mess my data.
I'm using GTM to deploy the GA code. In order to make the changes to the GA tracking code, I've used a Custom HTML Tag.
There is a slightly different implementation for gtag. You can set the following window property to true in the conditional statement:
window['ga-disable-GA_MEASUREMENT_ID'] = true;
Replace GA_MEASUREMENT_ID with the Analytics ID of the property that you would like to disable.
This window property must be set before any calls to gtag() are made, and it must be set on each page for which you want to disable Analytics. If the property is not set or set to false, then Analytics will work as usual.
More info in link below. Hope it helps.
gtag ga-diasble setting

Same web property ID is tracked twice

I'm using the following code in my page, in order to have Google Analytics and Tag Manager.
<script type="text/javascript">
(function(i, s, o, g, r, a, m) {i["GoogleAnalyticsObject"] = r;i[r] = i[r] || function() {(i[r].q = i[r].q || []).push(arguments)}, i[r].l = 1 * new Date();a = s.createElement(o),m = s.getElementsByTagName(o)[0];a.async = 1;
a.src = g;m.parentNode.insertBefore(a, m)})(window, document, "script", "//www.google-analytics.com/analytics.js", "ga");
ga('create', 'UA-xxxxx', 'auto');
ga("require", "displayfeatures");
ga('send', 'pageview');
</script>
and
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxx"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-xxxxxx');
</script>
So I'm getting warning "Same web property ID is tracked twice" in Google Tag Assistant.
Is there anyway I can fix this warning?
Actually you have two Google Analytics codes on your page and both are sending 'pageview' hits to Google Analytics. The first one is somewhat older analytics.js snippet and the second is a little bit newer gtag.js snippet. None of them has anything to do with Google Tag Manager.
Consider using just one, either analytics.js or gtag.js. Note that if you'll also install GTM and set up pageview tracking in GTM you'll get the same issue again because GTM will duplicate the existing analytics snippet functionality.

I cannot use Google Tag Manager, dataLayer push doesn't reflect anything on Analytics event panel

GTM snippet put after the GAnalytics one:
<!-- 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 -->
So I'm trying to run on console (from localhost, I tried also on live)
dataLayer.push({'event':'test'});
then when I look ad the Analytics event panel (real time) nothing appears on the graph. Any suggestion or idea why that's happening?
By default, Google Tag Manager doesn't send any data anywhere. When you do a dataLayer.push call, data is simply added to the dataLayer which sits in the client's browser.
For GTM to send data, you need to configure triggers and tags. If you want to setup GTM as passthrough so it sends whatever data you push to it to Google Analytics, here is an example:
https://www.simoahava.com/analytics/create-a-generic-event-tag/
Some general info about GTM:
Variables: they allow you to fetch data from the dataLayer
Triggers: they are rules that define when certain tags should be fired
Tags: they are pieces of code that can do whatever you want (add a banner to your website, send data to Google Analytics etc...).
dataLayer.push({'event':'test'});
It sends data to dataLayer, not to GA.
You can then create a GA Tag with trigger type "Custom event" using name of your event.
I sorted it out firing event straight from gtag (Google Analytics):
gtag('event', <action>, {
event_category: <category>,
event_label: <label>,
value: <value>
});

Google Analytics Gtag Multiple Analytics Account Tracking IDs

From what I can see Google seem to be phasing out analytics.js now in favor of their tag manager.
How do I fire google analytics new gtag tracking code for multiple analytics accounts?
Something like this:
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-108285779-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-108285779-2');
gtag('config', 'ANOTHER CODE');
gtag('config', 'ANOTHER CODE');
</script>
In short:
Yes, you can add the same type of product multiple times by calling gtag('config', ...) for each respective Google account + property ID you have.
Details:
It's 2021 and I had the same question but was too paranoid to trust this thread's top voted answer because it described a different experience than my own when testing how this works. Firstly, though, in order to answer OP's question in 2021 we have to look at two entries in Google's official docs, because neither entry fully answers the question but when brought together they can give us a bit more confidence in how to solve this:
Can I add more than one type of product using gtag('config', ...)? (Answer: yes.) (Docs)
Can I add more than one of the same type of product using gtag('config', ...)? (Answer: yes.) (Docs)
Here's an example snippet of how I accomplished OP's scenario using JavaScript. If you try this in your browser's console you should see a unique script get added for each ID you set in the below snippet's googleIds array.
Notes:
Notice that the snippet's googleIds array contains five IDs.
Notice that, after running the snippet in your browser console, five <script> tags get set to the page, but that the snippet itself only explicitly built and set one of the tags to the .
The rest of the tags get added after their respective IDs are pushed into the dataLayer, and after the first script is initialized (i.e. the element is constructed + set to the ). The order of these two steps doesn't matter (i.e. You can initialize first and then push your IDs to the dataLayer, or push your IDs to the dataLayer and then initialize).
// An array of IDs I want to load on the same page(s) at the same time
var googleIds = ["AW-00000000", "AW-00000001", "AW-00000002", "DC-00000000", "UA-00000000-1"];
// Setting dataLayer & gtag to window because I'm using a custom code text field in a tag management system
window.dataLayer = window.dataLayer || [];
window.gtag =
window.gtag ||
function() {
window.dataLayer.push(arguments);
};
window.gtag("js", new Date());
// Flag used to ensure script only set with first ID, and rest of IDs are pushed to dataLayer
var gtagScriptExists = false;
// ID validation regex. Only tested with AW-*, but DC & UA are also valid prefixes
var validIdStructure = new RegExp(/(AW|DC|UA)-[0-9]{8,}(-[0-9]{1,})?/);
// Push IDs into dataLayer and set initial gtag/js?id= script to page using first ID in googleIds array
for (var i = 0; i < googleIds.length; i++) {
var gtagId = googleIds[i];
// Validate that the ID being passed isn't a big weirdo
var idIsValid =
typeof gtagId === "string" && gtagId.match(validIdStructure);
if (idIsValid) {
window.gtag("config", gtagId);
// NOTE: gtag script only needs to be set to page once, but each gtag('config', <ID>) that's pushed to the dataLayer will add subsequent gtag/js?id=<ID> scripts to the page
if (!gtagScriptExists) {
// Set initial gtag/js?id=<first ID> script to <head>
var script = document.createElement("script");
script.type = "text/javascript";
script.async = true;
script.src = "//www.googletagmanager.com/gtag/js?id=" + gtagId;
document.getElementsByTagName("head")[0].appendChild(script);
// Update gtag/js?id= script status flag so this initialization script is only set for the first ID, and not all the IDs in the array
gtagScriptExists = true;
}
}
}
Yes, that is correct according to documentation. But it generated no data for me on the subsequent codes until I added
<script async src="https://www.googletagmanager.com/gtag/js?id=ANOTHER_CODE"></script>
Immediately above the code block. Either I stumbled on a working kludge or Google needs to update their documentation.
Yes. You can add multiple accounts, and send to all of them or send individually.
Setup
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXX-1">
</script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
// Global configs
gtag('config', 'G-XXXXXX-1');
gtag('config', 'AW-YYYYYY');
gtag('config', 'DC-ZZZZZZ');
</script>
And this is the place when firing events
<script>
// This is place firing event
// Send to all
// Send to one: Measure Google Ads conversions
gtag('event', 'conversion', {
'send_to': 'AW-YYYYYY/AbC-D_efG-h12_34-567',
'value': 1.0,
'currency': 'USD'
});
</script>
That seems to be the official way to do it according to the documentation.

Resources