Some days ago I introduced the new gtag version of Google Analytics to my private website. Now I try to figure how to filter out my own traffic. Exclusion based on IP is not possible because I enter my website with different browsers from different places. So I wanted to exclude my traffic via cookie. I'm just not able to make it work. The documentation is telling I should work now with dimensions. I tried it but it is not working for me.
I set up a dimension "usertype"
and I added a filter to exclude pattern "internal" for dimension "usertype"
I created a new page for my website and put the following code
<html>
<head>
<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXX-XX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments)};
gtag('js', new Date());
gtag('config', 'UA-XXXXXXX-XX', {'custom_map': {'dimension1': 'usertype'}});
gtag('event', 'kill_ga', {'usertype': 'internal'});
</script>
<!-- Google Analytics -->
<meta http-equiv="refresh" content="5; url=/" />
</head>
<body>
We'll transfer you soon
</body>
</html>
As I said before. I'd like to somehow mark my own traffic as internal and exclude it via filter.
Could someone help me to achieve this please?
There are many approaches to blocking your own traffic, I think using the usertype is one approach. Historically, I've solved it with a simple cookie check:
You'd ask any developer/tester to create a cookie on your site by 1) visiting your site, then 2) entering something like this into their browser's JS console:
Set the persistent cookie, "prevent_ga" equal to true
document.cookie = "prevent_ga=1"
Then check the cookie before invoking GA on your site:
var check_cookie = document.cookie.match(/^(.*;)?\s*prevent_ga\s*=\s*[^;]+(.*)?$/)
if (!check_cookie) {
// do gtag() things
}
I used it finally in a completely different approach.
As you can see on https://www.smest.it I solved with JavaScript directly on my page.
In footer of any page:
<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script>
var gaProperty = 'XXXXXXX-XX';
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
function gaOptout() {
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true;
}
</script>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXX-XX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){
dataLayer.push(arguments)
}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXX-XX', { 'anonymize_ip': true });
</script>
In my privacy policies, I have the following link.
<a onclick="alert('Google Analytics is now deactivated');" href="javascript:gaOptout()">Deactivate Google Analytics</a>
This will turn off analytics completely.
Related
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
This is turning out to be much more challenging than I expected.
There is a Google Analytics Integration extension:
https://www.mediawiki.org/wiki/Extension:Google_Analytics_Integration
But for the Global Site Tag version of Google Analytics, it refers you to the HeadScript extension:
https://www.mediawiki.org/wiki/Extension:HeadScript
The problem here is the download link (http://downloads.jingames.net/mediawiki/HeadScript.zip) looks a bit dodgy and the documentation mentions a bug.
I've also reviewed this discussion on the Mediawiki site:
How can I add the code into the <head>?
But it's a lot of telling and not much showing.
The Google Analytics snippet looks like this:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=TRACKING_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'TRACKING_ID');
</script>
I would prefer to insert it by updating a setting in LocalSettings.php config file.
Thanks!
I found a way to insert the tag into LocalSettings.php using the $wgHooks setting as demonstrated here:
https://www.mediawiki.org/wiki/Topic:Uxv32na6lh6wd5rf
Just replace TRACKING_ID (in both spots) with your Analytics tracking ID and paste this at the bottom of LocalSettings.php:
$wgHooks['BeforePageDisplay'][] = function( OutputPage &$out, Skin &$skin ) {
$code = <<<HTML
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=TRACKING_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'TRACKING_ID');
</script>
HTML;
$out->addHeadItem( 'gtag-insert', $code );
return true;
};
This is a very old post but for anyone else who might happen upon this I have found a possible solution by combining other examples.
I have used Tom's code as an example and replace the corresponding variables within the google analytics extension.
Change the hook in googleAnalytics.hooks.php line 9.
Old:
...
public static function onSkinAfterBottomScripts( Skin $skin, &$text = '' ) {
...
New:
...
public static function onBeforePageDisplay( OutputPage &$out, Skin $skin ) {
...
Then replace all mentions of $text with $code, there are 5 of them on lines, 14, 24, 31, 54, and 59.
Then at the end of the parser function above the return true; insert the following code on line 61.
$out->addHeadItem( 'gtag-insert', $code );
Finally go into googleAnalytics.php line 52 (Bottom of file) and replace the hook call to use the new hook.
Old:
...
$wgHooks['SkinAfterBottomScripts'][] = 'GoogleAnalyticsHooks::onSkinAfterBottomScripts';
...
New:
...
$wgHooks['BeforePageDisplay'][] = 'GoogleAnalyticsHooks::onBeforePageDisplay';
...
If everything worked out you will see the googleAnalytics extension script in the head element as opposed to within the body. I am going to try and contact the creator/contributors of the extension and see if it can be change or ask why its not this way already.
There is a google analytics extension.
Alternatively, the script can be added to Mediawiki:Common.js using document.write to link to the url and your site's UA-xxxxxx-x:
// GOOGLE ANALYTICS (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-xxxxxx-x', 'auto'); ga('send', 'pageview');
// END GOOGLE ANALYTICS
I am trying to get statistics about sign ups on my website.
The analytics code is in the header:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=...."></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', '....');
</script>
Then I have a form and sending it (with a page reload, not ajax) and in the success snippet in razor I placed the ga code:
#if (success)
{
<script type="text/javascript">
ga('send', 'event', 'Supplier sign up', 'click', '/signup/success');
</script>
<p>success message</p>
}
The success message appears and I can see the script in the page inspect but I don't get any stats in Google Analytics.
Am I missing something? In the past (a few years ago) I was using a similar code and it worked. Has anything changed recently? Do I need to enable something in the Analytics or my code is wrong?
I am following the documentation:
https://developers.google.com/analytics/devguides/collection/analyticsjs/events
https://developers.google.com/analytics/devguides/collection/analyticsjs/pages
The implementation method for your GA is through gtag.s, not GTM or analytics.js.
Thus you need to modify your success to:
#if (success)
{
<script type="text/javascript">
gtag('event', 'click', {'event_category': 'Supplier sign up', 'event_label': '/signup/success'});
</script>
<p>success message</p>
}
You have mixed two GA libraries. You use gtag.js for page view, so you need to use gtag.js (not analytics.js) for events.
https://developers.google.com/analytics/devguides/collection/gtagjs/events
Our goal here is to keep Google Analytics conversion goal tracking data when we load an iframe from another domain name that we own.
We have a domain rentalbookingsoftware.com that on the free trial page: https://rentalbookingsoftware.com/free-trial-signup/ loads an iframe for our free trial installer from sidev2.info like this:
<iframe width="80%" height="500" src="https://sidev2.info/freetrialform.php"></iframe>
We are trying to keep the gtag.js clientid the same but a new session is started on the sidev2.info gtag tracking which causes the correct referrer information to be lost as you can see from the gtag recording: https://www.screencast.com/t/l5wsqUF6oAg . Both sites are using this tracking code as suggested from: https://developers.google.com/analytics/devguides/collection/gtagjs/cross-domain and I have added sidev2.info to the referrer exclusion list: https://support.google.com/analytics/answer/2795830
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-45446232-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-45446232-1', {
'linker': {
'domains': ['rentalbookingsoftware.com', 'sidev2.info']
}
});
</script>
How can we keep the clientid using gtags? I found this: https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain but it is for analytics.js tracking, should we switch to that tracking method?