9

I have spent quite a lot of time researching ways to use GA on a locally run file, but not using http://localhost:(some_port).

Every method I use never returns any regular data. This is the closest I have got to receiving anything:

<script type="text/javascript">
            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-47519364-1']);
            _gaq.push(['_setDomainName', 'none']);
            _gaq.push(['_setAllowLinker', 'true']);
            _gaq.push(['_trackPageview']);

            (function() {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/u/ga_debug.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
            })();
        </script>

Anyone got this working in this fashion before?

Cœur
  • 37,241
  • 25
  • 195
  • 267
JohnRobertPett
  • 1,133
  • 4
  • 21
  • 36
  • I'm looking at the same question, and I think the answer is "you can't do that," although I'm not positive. However, you can definitely use competing services such as Keen.io when viewing a file locally with a file:// URL. – Harlan Aug 05 '14 at 16:43

1 Answers1

16

Thanks for enlightening my eyes with the option of the '_debug.js' script. I am using the analytics module and had the same problem. So I used the debug script and got the following error when trying to send an event:

Unallowed document protocol. Aborting hit.

I googled it and found this thread exlaining how to bypass it: https://productforums.google.com/forum/#!topic/analytics/KNz8TimivXo

This was the answer:

ga('create', 'UA-**********-6', {'cookieDomain': 'none'});
ga('set', 'checkProtocolTask', function(){ /* nothing */ });
ga('send', 'pageview');
ShlomiTC
  • 446
  • 4
  • 12
  • 1
    This workaround is also necessary in order to use Google Analytics in a Chrome extension. – jdunning Jan 05 '18 at 23:50
  • 2
    Also, you can just set `checkProtocolTask` to `null` instead of using a noop function. – jdunning Jan 06 '18 at 00:57
  • @jdunning this answer alone won't get analytics working for a Chrome extension. Refer to this answer instead https://stackoverflow.com/a/24165203 or this blog post https://davidsimpson.me/2014/05/27/add-googles-universal-analytics-tracking-chrome-extension/ – lasec0203 May 20 '18 at 02:16