Somewhat new to GA. I think I want custom dimensions and metrics but unclear how to implement this.
My app is behind a secure login page and once logged in each user is signed in to a tenant (company) and has a user ID. I want filter the activity for a particular tenant and / or user.
UPDATE
Here is the code the tag manager had me insert after setting it up (redacted my account into):
<!-- 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','XYZ-XXXXXX');</script>
<!-- End Google Tag Manager -->
Here is what I had before (the existing js and config tags) and added the additional 4 tags at the end). The <%= %> are Rails code tags.:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-B0KXZBVWXH">
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXX');
gtag('company_id', '<%= current_company.id %>');
gtag('company_name', '<%= current_company.name %>');
gtag('user_id', '<%= current_user.id %>');
gtag('username', '<%= current_user.name_full %>');
</script>
Here is my tag manager page:
I am trying to push this info to GA. I would like to generate reports etc and get a handle on which users and clients are using the site.
With GTM, you have to create a Google Analytics 4 Configuration tag and use Fields to Set to set persistent parameter values for all pages that fire the given tag. In your case, to set a user ID, add a row to Fields to Set and set the Field Name to user_id, and the Value to a Tag Manager Variable that returns the user ID.
https://support.google.com/tagmanager/answer/9442095?hl=en
The value of the user_Id have to pass to pushed to the dataLayer, or written in the page (in a JS variable) and then retrieve it, when you know it after the user login.
Related
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.
I had the current code in my WebSite, and it was working until a week ago:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-142497713-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXX-1');
gtag('set', {'content_group1': 'Guests'});
</script>
When my User log into my site the 'content_group1' becames 'My_User_type'.
I used to have charts filtered by content_group1 to see the typology of my users and how much they use my website.
Now it's not saving data of content_group1 and i cannot understand why. (I didn't change the footer of my page!)
Anyone have the same issue?
Thanks.
Dario.
You need to call the "set" before the "config". Also using content groups like this is messy, the intent for this dimension is to group the content (ie, support, product), not for what the user type is. You should look into setting a custom dimension OR use the userid feature for your use case.
gtag('set', {'content_group1': 'Guests'});
gtag('config', 'UA-XXXXXXX-1');
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
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.
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.