0

I have an html-file and want to add google analytics on it. After registering with ga I got the following tracking code to paste on my html file inside the head right at the beginning. I did that and then went on to my ga profile and there I chose the property and clicked on the "send test traffic" button. Unfortunately ga failed to access my website.

I thought, maybe it is because I'm trying to run the code on a local host. But according to Can you test google analytics on a localhost address? a local host is automatically recognised by ga. So I don't know what I'm doing wrong.

When I click on the "send test traffic" button, I get a new window that says "the website can not be accessed". I am using opera and firefox as browsers. got the same problem in both.

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



artre
  • 311
  • 4
  • 14
  • I guess that Google Analytics works on localhost when you access it with your browser (which can send requests to Google). But Google won't be able to _send test traffic_ to your localhost because they cannot reach it. – arkuuu Aug 22 '19 at 07:04

1 Answers1

3

Google Analytics works with localhost in so far as when you visit a site running on localhost, the browser can load the Google JS and send analytics data to Google.

The tool which generates test traffic for you won't work because, unlike your browser, Google can't make a request to your localhost URL — it is local to you.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Absolute URLs require schemes! – Quentin Oct 16 '20 at 10:25
  • 1
    Well, it has a separate `http://` or `https://` drop down list, can't give the absolute URL though. But doesn't matter, according to another post, they said any domain name should work for localhost, even the `https://example.com` will work. Thanks! – Antonio Ooi Oct 16 '20 at 10:54