From: Vincent Vanwaelscappel Date: Tue, 25 May 2021 15:21:30 +0000 (+0200) Subject: wait #4442 @3 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=cff93db791aeacf6dbc76ffa0110396611cc635d;p=fluidbook-html5.git wait #4442 @3 --- diff --git a/js/libs/fluidbook/fluidbook.stats.js b/js/libs/fluidbook/fluidbook.stats.js index 7b819d2a..2b199493 100644 --- a/js/libs/fluidbook/fluidbook.stats.js +++ b/js/libs/fluidbook/fluidbook.stats.js @@ -5,6 +5,7 @@ function FluidbookStats(fluidbook) { this.vid = guid(); this.relay_url_params = ''; this.lastTCHash = ''; + this.gaqueue = []; if (this.fluidbook.settings.stats) { this.worker = new Worker('js/libs/fluidbook/workers/stats.js'); } @@ -23,11 +24,7 @@ function FluidbookStats(fluidbook) { eval(this.fluidbook.settings.gtag_additional_code); } } else { - if ('ga' in window) { - this.ga = 'ga'; - } else { - this.ga = false; - } + this.checkGoogleAnalytics(); } if (this.fluidbook.settings.relay_url_params !== '') { var e = this.fluidbook.settings.relay_url_params.split(','); @@ -44,10 +41,7 @@ function FluidbookStats(fluidbook) { } FluidbookStats.prototype = { - - init: function () { - var $this = this; $(document).on('click', 'a[data-track]', function () { $this.track(6, 0, $(this).attr('data-track')); @@ -91,12 +85,37 @@ FluidbookStats.prototype = { this.trackGoogleAnalytics(type, page, extra); }, - trackGoogleAnalytics: function (type, page, extra) { - if (this.ga === false) { + checkGoogleAnalytics: function () { + if (this.ga !== false && this.ga !== undefined) { return; } + if ('gtag' in window) { + this.ga = 'gtag'; + } else if ('ga' in window && "getAll" in window.ga) { + this.ga = 'ga'; + } else { + this.ga = false; + } - var url = location.pathname + location.search + location.hash + if (this.ga !== false && this.gaqueue.length > 0) { + var $this = this; + $.each(this.gaqueue, function (k, v) { + $this.trackGoogleAnalytics(v[0], v[1], v[2], v[3]); + }); + this.gaqueue = []; + } + }, + + trackGoogleAnalytics: function (type, page, extra, url) { + this.checkGoogleAnalytics(); + + if (url === undefined) { + url = location.pathname + location.search + location.hash + } + if (this.ga === false) { + this.gaqueue.push([type, page, extra, url]); + return; + } switch (type) { case 0: // Change page @@ -183,8 +202,12 @@ FluidbookStats.prototype = { } } } else if (this.ga === 'ga') { - args.unshift('send'); - window['ga'].apply(null, args); + $.each(ga.getAll(), function (k, tracker) { + try { + tracker.send.apply(tracker, args); + } catch (e) { + } + }); } },