jeudi 10 novembre 2016

what does the temporary "ga" variable do in the Google Analytics script?

(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', 'https://www.google-analytics.com/analytics.js', 'ga');

This is the typical GA script. What I don't understand is what these lines do:

   i[r] = i[r] || function() {
        (i[r].q = i[r].q || []).push(arguments)
    }, i[r].l = 1 * new Date();

It names a ga(window['ga']) function, but only temporarily, because after the analytics.js is loaded, apparently this "ga" function has been replaced. And I've tried removing this line, there seems to be no immediate javascript error(though I haven't tested if it has an impact on the data collected by google).

I just want to know whether this "seemed useless" line is there because of some coding convention.

For the i[r].l, I understand that it record the timestamp of when this temporary ga function is created. But for pushing all the arguments into i[r].q, I don't see the reason to do this.

Aucun commentaire:

Enregistrer un commentaire