I have added the JavaScript that I need to the bottom of my pages so that I can make use of Google Analytics. Only problem is that I am sure that it is counting all my devel
You can use this code
<script>
var host = window.location.hostname;
if(host != "localhost")
{
// your google analytic code here
}
</script>
I have a PHP variable set for my local development that gives me a terminal for providing data/feedback etc when I'm working on stuff.
I use XAMPP so that has an env variable for tmp which is the following:
$isLocal = (getenv("tmp") == '\xampp\tmp') ? true : false;
This doesn't exist on my production server because xampp is not being used
if($isLocal){
// do something, eg. load my terminal
}
... Specific to this question:
<?php if(!$isLocal){ ?>
<!-- Insert Google Analytics Script Here -->
<?php } // end google analytics local check ?>
I know this post is super old, but none of the solutions met my needs. Not only did I want to remove dev work from GA (and FB), but I also wanted to have some folks within the company not be counted in GA and FB. So I wanted a relatively easy method for those folks to exclude themselves from analytics without a plugin, or ruling out a domain ip (as folks with laptops wander).
I created a webpage that users can go to and click a link to opt out of the GA and FB tracking. It places a cookie for the site. Then I check that cookie to determine if we should send data to GA and FB.
I originally set this up on a site for called Dahlia, which is a boutique maker of items for Greek Orthodox Weddings and Baptisms.
Here's the code:
I put the following code in the header for all web pages:
<script>
//put in your google analytics tracking id below:
var gaProperty = 'UA-XXXXXXXX-X';
// Disable tracking if the opt-out cookie exists.
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
window['ga-disable-UA-7870337-1'] = true; //This disables the tracking on Weebly too.
} else {
//put in your facebook tracking id below:
fbq('init', 'YYYYYYYYYYYYYYY');
fbq('track', 'PageView');
}
</script>
Be sure to add your GA and FB tracking IDs in the spaces provided. This was originally written for a Weebly (shopping CMS) site. So if you are not on Weebly you can remove the line that mentions weebly.
Then I created a new webpage called "do-not-track" with the following code in the header:
<script>
//put in your own google analytics tracking id below:
var gaProperty = 'UA-XXXXXXXX-X';
var disableStr = 'ga-disable-' + gaProperty;
// Opt-out function
function gaOptout() {
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true;
gaOptoutCheck();
}
// Check Opt-out function
function gaOptoutCheck() {
var name = "ga-disable-"+gaProperty+"=";
var ca = document.cookie.split(';');
var found = "false";
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) found = "true";
}
if (found == "true") alert("Cookie is properly installed");
else alert("COOKIE NOT FOUND");
}
</script>
And the following code in the body:
<a href="javascript:gaOptout()">Click here to opt-out of Google and Facebook Analytics</a>
<br><br>
Please visit this page on every computer, laptop, phone, tablet, etc. that you use;
and for all browser you use on each of those devices.
<br><br>
If you ever remove cookies from browser, you will need to repeat this process for that browser.
<br><br><br>
<a href="javascript:gaOptoutCheck()">
Click to check if cookie is set</a>
<br><br>
Here is my full writeup for the Weebly site
Hope this helps somebody!
If you have a react application and you have ejected the app(this could work for CRA as well). You can make use of the below code snippet in the index.html
page.
<script type="text/javascript">
if("%NODE_ENV%"==="production"){
//your analytics code
}
If you're not using static IP, setting IP filters on GA can't help you.
Set an environment variable and conditionally display it. Take the following Ruby on Rails code, for instance:
<% unless RAILS_ENV == "development" %>
<!-- your GA code -->
<% end %>
You can extend this behavior every language/framework you use on any operating system. On PHP, you can use the getenv function. Check it out the Wikipedia page on Environment Variables to know how to proceed on your system.
Use a custom metric to filter all this traffic.
When you init GA in your app, set a custom flag to track developres:
// In your header, after the GA code is injected
if( <your_code_to_check_if_is_dev> ) {
ga('set', 'is_developer', 1 );
}
Then add a filter in your GA Account to remove these results.
Admin > Account > All Filters > Add Filter > User Defined