Reputation: 15695
Google has completely changed analytics, ga.js has been replaced by analytics.js and non of the old methods listed below work anymore:
Method1:
<body onLoad="javascript:pageTracker._setVar('test_value');">
Method2:
<body onLoad="javascript:_gaq.push(['_setVar','test_value']);">
Method3:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setVar', 'exclude_me']);
_gaq.push(['_setAccount', 'UA-xxxxxxxx-x']);
_gaq.push(['_trackPageview']);
// etc...
</script>
The new GA snipplet looks like this:
<script>
(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-XXXX-Y'); // Creates a tracker.
ga('send', 'pageview'); // Sends a pageview.
</script>
I checked the documentation but I couldn't figure out how to set a variable that can be used in the filter, to exlude internal traffic from Google Analytics.
Any ideas?
Upvotes: 0
Views: 1462
Reputation: 19
I almost gave up searching for answer, but then found this section in the documentation.
Make yourself a dummy page on your site. For example: www.yourdomain.com/exclude-traffic.html
Add in <meta name="robots" content="noindex, nofollow"> in head, so the page wont be indexed.
Add script above your tracking code inside the head tag. Make sure you update UA-XXXXXXXX-Y values. You can check your GA code in Admin -> Tracking info -> Tracking code
Add link inside body tag.
Open the page with your browsers and click on the link. (You might be using Firefox and Chrome etc.)
Final result:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Remove My Internal Traffic from Google Analytics</title>
<meta name="robots" content="noindex, nofollow">
<script>
// Set to the same value as the web property used on the site
var gaProperty = 'UA-XXXXXXXX-Y';
// Disable tracking if the opt-out cookie exists.
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
// Opt-out function
function gaOptout() {
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true;
}
</script>
<script>
(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-XXXXXXXX-Y', 'domainname.com'); //now its changed to auto
ga('send', 'pageview');
</script>
</head>
<body>
<h1>Remove My Internal Traffic from Google Analytics</h1>
<p><a href="javascript:gaOptout()">Click here to opt-out of Google Analytics</a></p>
</body>
</html>
Upvotes: 0
Reputation: 2452
You should add it as a set call, after the create call and before a send call.
ga('set', 'dimension1', 'internal');
So in your case,
<body onLoad="javascript:ga('set','dimension1','internal');">
This will then associate that dimension (1) with the rest of the send calls made on that page. To add a filter on a view, you'll need to have the dimension setup already, then select a "Custom Filter" > "Exclude" > Filter Field should be set to your custom dimension (usually at the very end of the list).
And while that method is preferred as you can add or remove filters later on to capture that traffic, you can also use the opt-out setting to remove that traffic:
window['ga-disable-UA-XXXX-Y'] = true;
Where UA-XXXX-YY
is your account ID.
Upvotes: 4
Reputation: 32770
While link-only answers are generally frowned upon this is covered in the Universal Analytics documentation, so I suggest you look at Universal Analytics - Advanced Configuration - User Opt Out.
UPDATE
Sorry, missed that last sentence about filtering. It is possible to filter via custom dimensions (but only after you created them in the backend), so you'd need to follow these steps:
Create a custom dimension - let's call it "opt-out" - in the property settings.
Send a value for that dimension via the trackingcode:
ga('send', 'pageview', {
'dimension1': 'true'
});
Where the dimension is indicated by the literal string "dimension" and the numeric index (which is shown in the custom dimension dialog in the property settings).
Then chose a view, go to filters, choose new filter/advanced/exclude, choose custom dimensions "opt-out" in the filter field and "true" in filtern pattern (presumably you have done the same with custom vars before, so it should be familiar).
Upvotes: 2
Reputation:
It appears from the upgrade documentation that you'd need to set some custom variables and use those in creating some filters (see this post).
That said, it seems fairly complicated to me. An easier solution from my perspective is to use a server-side language which outputs the Google Analytics code only if the user is not in an exclude list.
Your tags don't provide a server-side language (ok, javascript can be used server-side, but I'm not thinking that's where you're at with this question), so can't provide a demo for you in a language I know you have, but in PHP, I would write it like this:
<?php if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1') {?>
<script>
(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-XXXX-Y'); // Creates a tracker.
ga('send', 'pageview'); // Sends a pageview.
</script>
<?php } ?>
Upvotes: 0