When I view the page source of my website, I can see my Google Analytics snippet:

 <script async src="https://www.googletagmanager.com/gtag/js?id=UA-12367889-1"></script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-12367889-1');
 gtag('config', 'AW-608708765');

I tried pasting this code into a local file and opening that file in a browser. Google Analytics counted the hit that wasn't from my website.

That means everyone can copy my code and use for fake traffic. How can I prevent that?

  • 2
    GA has had issues in the last few years with fake traffic, especially spam injected into GA reports. See our tag google-analytics-spam for bunch of questions folks have asked about it. May 14, 2018 at 11:51

1 Answer 1


One solution is to set up a 'Custom Filter' to only include data from your domain. This filter should then prevent your data from being corrupted if someone copies and uses your tracking snippet on another domain.

So, it would be something like this:

  1. Admin
  2. Filters
  3. Add Filter
  4. Pic a name
  5. Choose 'Custom'
  6. Choose 'Include'
  7. Choose 'Hostname' in the dropdown
  8. Write your hostname with a \ before the dot, like this \.com
  9. Click Save

Here's two blog post describing the solution:




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.