25

I use this for google analytics,

<noscript>
<iframe src="//www.googletagmanager.com/ns.html?id=GTM-KCQGLT" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>

and this

<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 =
    '//www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);})(window, document, 'script', 'dataLayer', 'GTM-KCQGLT');</script>

Can I disable this script when I run this code in localhost?

2
  • 1
    just comment it Commented Oct 28, 2016 at 3:51
  • 2
    Comment it or if(/localhost/.test(window.location.hostname)) return Commented Oct 28, 2016 at 3:56

4 Answers 4

58

Use below code.

<script>
var host = window.location.hostname;
if(host != "localhost")
{
    // your google analytic code here
}
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

By far the best comment. Simple and gets the job done. Thanks,
25

As described here, this is the correct way to prevent further events being sent to google analytics, even after ga(...) has been initialized:

if (admin || localhost) { // disable GA:
  window['ga-disable-UA-XXXXX-Y'] = true; // enter your tracking ID
}

Now any further calls to ga(...) will have no effect.

Quoting the docs:

The analytics.js library includes a window property that, when set to true, disables analytics.js from sending data to Google Analytics. When Google Analytics attempts to set a cookie or send data back to the Google Analytics servers, it will check whether this property is set to true. If it is, no action will be taken.

To disable tracking, set the following window property to true:

window['ga-disable-UA-XXXXX-Y'] = true;

Where the value UA-XXXXX-Y corresponds to the property ID on which you would like to disable tracking.

4 Comments

Please don't add the same answer to multiple questions. Answer the best one and flag the rest as duplicates, once you earn enough reputation. If it is not a duplicate, tailor the post to the question and flag for undeletion.
This should be the correct answer since it correctly references the literature and code directly from google's documentation.
3

go to Analytics Settings, edit your site, and +Add Filter to define a filter that excludes your IP address.

1 Comment

This doesn't stop the analytics executing. It only "filters" out any data on the server that originated from 'localhost'.
0

The accepted answer did not work for me, but a comment by @Baruch under the original question did the trick.

if (/localhost/.test(window.location.hostname)) {
    return
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.