For Ruby, someone in the above post’s comments suggested the creation of an action in the controller for when an admin visits the dashboard and then add a script to the Google Analytics tag.
For people trying this in ruby;
Add to a controller action that triggers when you visit your dashboard;
cookies[‘is_admin?’] ||= {
value: true,
expires: 10.years.from_now
}
Add to your google analytics script tag;
<% if cookies[‘is_admin?’] == ‘true’ %>
ga(‘set’,‘dimension1’,‘true’);
<% end %>
How to go about adding that action to the controller?
Alternatively, Anyone with insights on how to best exclude admin traffic from Google Analytics?
Thanks for sharing @rdyar.
This is the one I’ve also used previously, but sadly that doesn’t help when I test the build in other browsers or on mobile browsers.
This is the reason I was trying to play with cookies.
Still not resolved.
The hack below only works to stop my own traffic from being tracked in Google Chrome. My traffic is still being tracked everywhere else
This is how far I’ve gotten:
Acc. to Google’s own documentation for a legacy library for tracking here:
To disable tracking, set the following window property to true:
window[‘ga-disable-UA-XXXXXX-Y’] = true;
This property must be set on each page where you want to disable Google Analytics tracking
So, I’ve added the below before my Global Site Tag (gtag.js) tracking code:
<script>
// Set to the same value as the web property used on the site
var gaProperty = 'UA-XXXX-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>
Then I’ve added an new HTML link to a special/secret page to execute the opt-out logic:
<a href="javascript:gaOptout()">Click here to opt-out of Google Analytics</a>
But, as I’ve mentioned above, this only works in Goggle Chrome (it stops my traffic from being tracked in Chrome). My traffic, however, is still being tracked in all other desktop browsers and mobile browsers despite me going into each and clicking the “click here to opt-out of Google Analytics” link.
{% if site.google_analytics and jekyll.environment == 'production' %}
{% include analytics.html %}
{% endif %}
This basically tells Jekyll not to include the google analytics script when in development mode. Analytics will be included when you build your site for production using
The thing is that I want to stop my own traffic from being tracked in production. And I want to do that across devices and browsers. So it allows me to test the build on mobile phones, on different browsers etc. without messing up my analytics.
This is the reason (the hack I’ve posted above) was playing cookies, but maybe there is some other way of going about it.
Deploy Previews work by deploying every pull request from your git repository to a unique URL; completely different from the one your main site uses. You and your team can see how those changes look before they’re merged into the main branch and deployed to production.
This way you could still use my tutorial above to remove google analytics from your build also. bundle exec jekyll build = no analytics. JEKYLL_ENV=production bundle exec jekyll build = include analytics.
Interesting solution. Thanks for sharing.
I’m however not using a CMS. And wanted to continue not using one, if I can
Will explore a little longer and report back, should I find a way.
For now though, my hack above works for Chrome (problem is I can’t figure why it only works for Chrome, but not for Edge, Firefox, Chrome mobile, Safari mobile, or Brave mobile )
For others in the future: This is how I got this to work:
Create a special hidden/secret page with the below (and instruct the robots not to index it):
<html>
<head>
<meta name="robots" content="noindex, nofollow">
<script>
// Set to the same value as the web property used on the site
var gaProperty = 'UA-XXXXXXX-1';
// 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>
</head>
<body>
Don't do anything here unless instructed to by a dev (aka Pat).
<br>
<a onclick="alert('Google Analytics Tracking is now deactivated');" href="javascript:gaOptout()">Click here to opt-out of Google Analytics</a>
</body>
</html>
Add the script below above your tracking code (from Google Analytics) (do this wherever you hold your tracking code)
<script>
// Set to the same value as the web property used on the site
var gaProperty = 'UA-XXXXX-1';
// Disable tracking if the opt-out cookie exists.
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
</script>
With the above implemented, you only have to visit the special/hidden page in all your browsers/devices and click the link to place the “do not track me” cookie in your browser.
Cons: You have to remember to do this again whenever you clear your cookies.