Search code examples
google-analyticsgoogle-analytics-apigtag.js

Google Analytics - domain setting required for test?


I am trying to set up a test page for Google Analytics. I selected the web option for the project, specified the company url as a placeholder. We will not run the GA code on the page, but I had to fill something as it was asking. Pasted the gtag code into a test page to test it.

Open in browser on my machine, see no traffic on the Live analytics tracking page.

Question - does the tracking have to happen on a page of the domain? Or, can it come from anywhere (my desktop)? I use GA in a different project for mobile apps where events work, but this is a internal web app and it's failing at the minimum code.

<html>
    <head>
        <!-- Global site tag (gtag.js) - Google Analytics -->
        <script async src="https://www.googletagmanager.com/gtag/js?id=UA-123456789-1"></script>
        <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'UA-123456789-1');
        gtag('event', 'TEST');
        </script>
    </head>
    <bod>
        HELLO WORLD
    </bod>
</html>

Solution

  • Analytics snippet need to set a cookie to work, otherwise if you try to open an html page from desktop you will receive an error in the console: Cookie write failed.

    If you try to copy and run your code in an online tool for script testing, like https://jsfiddle.net/, you will see that the tracking works correctly (the pageview and event is sent to the Analytics Property).

    If you created the Analytics View recently it is likely that it will take some time before seeing the results (even in real time).