Q:

Google analytics on localhost: Why is my test code not working?

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>

A:

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.

