1

I want to do some operations if the Google analytics is blocked by the user (may be by the browser).

I am getting a lot of answers here if it is using ga.js, but I am including the analytics.js.

When I tried (typeof ga) = function (always irrespective of Google analytics is blocked or not by the browser. But event is not tracked if it is blocked.)

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
          (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','//www.google-analytics.com/analytics.js','ga');       

ga('create', 'google_analytics_id here', 'auto');

Any help to track whether you are actually calling the analytics.js.

Thank You

Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
Arun Pati
  • 125
  • 1
  • 9
  • This is quite possibly a duplicate of http://stackoverflow.com/questions/29282313/check-if-analytics-js-is-loaded#comment46762826_29282313 – Eike Pierstorff Apr 16 '15 at 10:01

1 Answers1

0

I'll have to see the error message, but try replacing it with this updated version.


    (function(b, o, i, l, e, r) {
        b.GoogleAnalyticsObject = l;
        b[l] || (b[l] =
            function() {
                (b[l].q = b[l].q || []).push(arguments)
            });
        b[l].l = +new Date;
        e = o.createElement(i);
        r = o.getElementsByTagName(i)[0];
        e.src = '//www.google-analytics.com/analytics.js';
        r.parentNode.insertBefore(e, r)
    }(window, document, 'script', 'ga'));
    ga('create', 'google_analytics_id here');
    ga('send', 'pageview');

LaurentiuZ
  • 61
  • 1
  • 2
  • Hi Laurentiu, I am not getting any error. If the bowser has blocked the Google analytics access, is there any way to check that we are not going to track anything here using JS? – Arun Pati Apr 16 '15 at 10:19
  • @ArunPati, look in the question linked in my comment to your question. There is a function to tell you if a GA tracker is successfully initiated. – Eike Pierstorff Apr 16 '15 at 12:27