From f953d068b1da3d73c73caecdca9562994b50e06f Mon Sep 17 00:00:00 2001 From: Stephen Cameron Date: Wed, 21 Sep 2022 18:17:12 +0200 Subject: [PATCH] Added a small note about Matomo stats for future reference. --- js/libs/fluidbook/fluidbook.stats.js | 752 +++++++++++++-------------- 1 file changed, 376 insertions(+), 376 deletions(-) diff --git a/js/libs/fluidbook/fluidbook.stats.js b/js/libs/fluidbook/fluidbook.stats.js index a6867d0a..34f12380 100644 --- a/js/libs/fluidbook/fluidbook.stats.js +++ b/js/libs/fluidbook/fluidbook.stats.js @@ -1,376 +1,376 @@ -function FluidbookStats(fluidbook) { - var $this = this; - this.fluidbook = fluidbook; - this.id = this.fluidbook.settings.id; - 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'); - } - if (this.fluidbook.settings.statsMatomo) { - if (this.fluidbook.settings.statsMatomoServer === undefined || this.fluidbook.settings.statsMatomoServer === null) { - this.fluidbook.settings.statsMatomoServer = '3'; - } - window._paq = window._paq || []; - window._paq.push(["setDoNotTrack", true]); - window._paq.push(['enableHeartBeatTimer']); - window._paq.push(['enableLinkTracking']); - (function (siteId, server) { - var u = "https://stats" + server + ".fluidbook.com/"; - window._paq.push(['setTrackerUrl', u + 'matomo.php']); - window._paq.push(['setSiteId', siteId]); - var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0]; - g.async = true; - g.src = u + 'matomo.js'; - s.parentNode.insertBefore(g, s); - })(this.fluidbook.settings.statsMatomo, this.fluidbook.settings.statsMatomoServer.toString()); - } - if (this.fluidbook.settings.googleTagManager !== undefined && this.fluidbook.settings.googleTagManager !== '') { - this.ga = 'gtm'; - } else if (this.fluidbook.settings.googleAnalytics !== '') { - this.ga = 'gtag'; - this.gaCodes = this.fluidbook.settings.googleAnalytics.split(','); - if (window.gtag !== undefined) { - gtag('js', new Date()); - } - $.each(this.gaCodes, function (k, code) { - if (window.gtag !== undefined) { - gtag('config', code, {'send_page_view': false}); - } - }); - if (this.fluidbook.settings.gtag_additional_code !== '') { - eval(this.fluidbook.settings.gtag_additional_code); - } - } else { - this.checkGoogleAnalytics(); - } - if (this.fluidbook.settings.relay_url_params !== '') { - var e = this.fluidbook.settings.relay_url_params.split(','); - var p = []; - $.each(e, function (k, v) { - v = v.trim(); - if ($_GET[v] !== undefined && $_GET[v] !== null) { - p.push(v + '=' + encodeURIComponent($_GET[v])); - } - }); - this.relay_url_params = p.join('&'); - } - this.init(); -} - -FluidbookStats.prototype = { - init: function () { - var $this = this; - $(document).on(this.fluidbook.input.clickEvent, 'a[data-track]', function () { - $this.track(6, 0, $(this).attr('data-track')); - return true; - }); - - if (this.fluidbook.settings.tagcommander_id) { - this.initTagCommander(); - } - }, - - initTagCommander: function () { - var $this = this; - $(this.fluidbook).on('fluidbook.hashchange', function (e, data) { - if (window.location.hash === this.lastTCHash) { - return; - } - this.lastTCHash = window.location.hash; - - data.shift(); - var hashes = []; - if (!$this.fluidbook.displayOnePage && data[0] == 'page') { - var p = parseInt(data[1]); - if (!isNaN(p)) { - var even, odd; - if (p % 2 === 0) { - even = p; - odd = p + 1; - } else { - odd = p; - even = p - 1; - } - if ($this.fluidbook.settings.tagcommander_plan['page/' + even] !== undefined) { - data[1] = even; - hashes.push(data.join('/')); - } - - if ($this.fluidbook.settings.tagcommander_plan['page/' + odd] !== undefined) { - data[1] = odd; - hashes.push(data.join('/')); - } - } - } else { - hashes.push(data.join('/')); - } - - var custom_vars = {}; - if (window.location.toString().indexOf('workshop.fluidbook.com') >= 0) { - custom_vars.env_work = 'pre-prod'; - } - - - $.each(hashes, function (k, hash) { - if ($this.fluidbook.settings.tagcommander_plan[hash]) { - setTimeout(function () { - window.tc_vars = {}; - var location = window.location.toString().split("#")[0]; - location += '#/' + hash; - custom_vars.url = location; - - $.extend(window.tc_vars, $this.fluidbook.settings.tagcommander_default_vars, custom_vars, $this.fluidbook.settings.tagcommander_plan[hash]); - console.info('Tag commander', window.tc_vars); - try { - var o = {events: {}}; - o.events[$this.fluidbook.settings.tagcommander_event_page] = [{}, {}]; - tC.container.reload(o); - } catch (e) { - console.error(e); - } - }, 500 * k) - - } - }); - - - }); - }, - - track: function (type, page, extra) { - if (!this.fluidbook.support.hasNetwork() || this.fluidbook.nointerface) { - return; - } - - if (this.fluidbook.settings.stats) { - try { - this.trackFluidbook(type, page, extra); - } catch (e) { - - } - } - if (this.fluidbook.settings.statsMatomo) { - try { - this.trackMatomo(type, page, extra); - } catch (e) { - - } - } - try { - this.trackGoogleAnalytics(type, page, extra); - } catch (e) { - console.log(e); - } - }, - - 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; - } - - 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 = []; - } - }, - - trackMatomo(type, page, extra, url) { - if (url === undefined) { - url = window.location; - } - switch (type) { - case 0: // Change page - window._paq.push(['setCustomUrl', '/page/' + page]); - window._paq.push(['trackPageView', 'View page ' + page]); - break; - case 10: // Open Book - window._paq.push(['setCustomUrl', '/']); - window._paq.push(['trackPageView', 'Open fluidbook']); - break; - case 14: // Open chapters menu - window._paq.push(['trackEvent', 'menu', 'click', 'chapters']); - break; - case 6: // Click a link in a page - window._paq.push(['trackEvent', 'link', 'click', extra]); - window._paq.push(['trackLink', extra]); - break; - case 1: // Search - window._paq.push(['trackSiteSearch', extra]); - break; - case 3: // Print - window._paq.push(['trackEvent', 'menu', 'click', 'print']); - break; - case 5: // Share - case 12: // Facebook - case 13: // Twitter - window._paq.push(['trackEvent', 'share', 'send', extra]); - break; - case 7: // PDF - case 8: // Partial PDF - window._paq.push(['trackEvent', 'menu', 'click', 'download']); - break; - case 4: // Bookmarks - window._paq.push(['trackEvent', 'bookmark', 'page', 'page ' + page]); - break; - case 9: // Extras - window._paq.push(['trackEvent', 'extra', 'click', extra]); - break; - case 2:// Zoom - window._paq.push(['trackEvent', 'zoom', 'page', page]); - break; - case 11:// Video - window._paq.push(['trackEvent', 'video', 'view', extra]); - break; - case 15:// - window._paq.push(['trackEvent', 'cart', 'addproduct', extra]); - } - }, - - 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 - this._ga('pageview', url); - break; - case 10: // Open Book - this._ga('pageview'); - break; - case 14: // Open chapters menu - this._ga('event', 'menu', 'click', 'chapters'); - break; - case 6: // Click a link in a page - this._ga('event', 'link', 'click', extra); - break; - case 1: // Search - var l = document.createElement('a'); - l.href = window.location.toString(); - if (l.search == '') { - l.search = '?'; - } else { - l.search += '&'; - } - l.search = 'q=' + extra; - this._ga('pageview', l.pathname + l.search + l.hash); - break; - case 3: // Print - this._ga('event', 'menu', 'click', 'print'); - break; - case 5: // Share - case 12: // Facebook - case 13: // Twitter - this._ga('event', 'share', 'send', extra); - break; - case 7: // PDF - case 8: // Partial PDF - this._ga('event', 'menu', 'click', 'download'); - break; - case 4: // Bookmarks - this._ga('event', 'bookmark', 'page', 'page ' + page); - break; - case 9: // Extras - this._ga('event', 'extra', 'click', extra); - break; - case 2:// Zoom - this._ga('event', 'zoom', 'page', page); - break; - case 11:// Video - this._ga('event', 'video', 'view', extra); - break; - case 15:// - this._ga('event', 'cart', 'addproduct', extra); - } - }, - - _ga: function (a0, a1, a2, a3, a4) { - console.log('_ga', a0, a1, a2, a3, a4); - var args = Array.prototype.slice.call(arguments); - if (this.ga === 'gtm') { - var dl = {}; - if (a0 === 'pageview') { - dl.event = 'pageview'; - if (a1 !== undefined) { - dl.location = a1; - } - } else if (a0 === 'event') { - dl.event = a1; - dl.eventLabel = a3; - dl.eventAction = a2; - } - dataLayer.push(dl); - } else if (this.ga === 'gtag') { - var f, o, a; - o = {}; - if (a0 === 'pageview') { - f = 'event'; - o = {}; - a = 'page_view'; - if (a1 !== undefined) { - o.page_path = a1; - } - } else if (a0 === 'event') { - f = 'event'; - a = a2; - o.event_category = a1; - o.event_label = a3; - } - - $.each(this.gaCodes, function (k, code) { - var data = $.extend({send_to: code}, o) - gtag(f, a, data); - }); - } else if (this.ga === 'ga') { - $.each(ga.getAll(), function (k, tracker) { - try { - tracker.send.apply(tracker, args); - } catch (e) { - } - }); - } - }, - - trackFluidbook: function (type, page, extra) { - if (page == undefined) { - page = 0; - } - if (extra == undefined) { - extra = ''; - } - - var $this = this; - - - var data = { - id: $this.id, vid: $this.vid, type: type, page: page, str: extra, time: new Date().getTime() - }; - - setTimeout(function () { - $this.fluidbook.networkControl.executeWhenNetwork(function (cb) { - $this.worker.postMessage(data); - setTimeout(cb, 100); - }, false); - }, 2500); - } -}; - +function FluidbookStats(fluidbook) { + var $this = this; + this.fluidbook = fluidbook; + this.id = this.fluidbook.settings.id; + 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'); + } + if (this.fluidbook.settings.statsMatomo) { + if (this.fluidbook.settings.statsMatomoServer === undefined || this.fluidbook.settings.statsMatomoServer === null) { + this.fluidbook.settings.statsMatomoServer = '3'; + } + window._paq = window._paq || []; + window._paq.push(["setDoNotTrack", true]); + window._paq.push(['enableHeartBeatTimer']); + window._paq.push(['enableLinkTracking']); + (function (siteId, server) { + var u = "https://stats" + server + ".fluidbook.com/"; + window._paq.push(['setTrackerUrl', u + 'matomo.php']); + window._paq.push(['setSiteId', siteId]); + var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0]; + g.async = true; + g.src = u + 'matomo.js'; + s.parentNode.insertBefore(g, s); + })(this.fluidbook.settings.statsMatomo, this.fluidbook.settings.statsMatomoServer.toString()); + } + if (this.fluidbook.settings.googleTagManager !== undefined && this.fluidbook.settings.googleTagManager !== '') { + this.ga = 'gtm'; + } else if (this.fluidbook.settings.googleAnalytics !== '') { + this.ga = 'gtag'; + this.gaCodes = this.fluidbook.settings.googleAnalytics.split(','); + if (window.gtag !== undefined) { + gtag('js', new Date()); + } + $.each(this.gaCodes, function (k, code) { + if (window.gtag !== undefined) { + gtag('config', code, {'send_page_view': false}); + } + }); + if (this.fluidbook.settings.gtag_additional_code !== '') { + eval(this.fluidbook.settings.gtag_additional_code); + } + } else { + this.checkGoogleAnalytics(); + } + if (this.fluidbook.settings.relay_url_params !== '') { + var e = this.fluidbook.settings.relay_url_params.split(','); + var p = []; + $.each(e, function (k, v) { + v = v.trim(); + if ($_GET[v] !== undefined && $_GET[v] !== null) { + p.push(v + '=' + encodeURIComponent($_GET[v])); + } + }); + this.relay_url_params = p.join('&'); + } + this.init(); +} + +FluidbookStats.prototype = { + init: function () { + var $this = this; + $(document).on(this.fluidbook.input.clickEvent, 'a[data-track]', function () { + $this.track(6, 0, $(this).attr('data-track')); + return true; + }); + + if (this.fluidbook.settings.tagcommander_id) { + this.initTagCommander(); + } + }, + + initTagCommander: function () { + var $this = this; + $(this.fluidbook).on('fluidbook.hashchange', function (e, data) { + if (window.location.hash === this.lastTCHash) { + return; + } + this.lastTCHash = window.location.hash; + + data.shift(); + var hashes = []; + if (!$this.fluidbook.displayOnePage && data[0] == 'page') { + var p = parseInt(data[1]); + if (!isNaN(p)) { + var even, odd; + if (p % 2 === 0) { + even = p; + odd = p + 1; + } else { + odd = p; + even = p - 1; + } + if ($this.fluidbook.settings.tagcommander_plan['page/' + even] !== undefined) { + data[1] = even; + hashes.push(data.join('/')); + } + + if ($this.fluidbook.settings.tagcommander_plan['page/' + odd] !== undefined) { + data[1] = odd; + hashes.push(data.join('/')); + } + } + } else { + hashes.push(data.join('/')); + } + + var custom_vars = {}; + if (window.location.toString().indexOf('workshop.fluidbook.com') >= 0) { + custom_vars.env_work = 'pre-prod'; + } + + + $.each(hashes, function (k, hash) { + if ($this.fluidbook.settings.tagcommander_plan[hash]) { + setTimeout(function () { + window.tc_vars = {}; + var location = window.location.toString().split("#")[0]; + location += '#/' + hash; + custom_vars.url = location; + + $.extend(window.tc_vars, $this.fluidbook.settings.tagcommander_default_vars, custom_vars, $this.fluidbook.settings.tagcommander_plan[hash]); + console.info('Tag commander', window.tc_vars); + try { + var o = {events: {}}; + o.events[$this.fluidbook.settings.tagcommander_event_page] = [{}, {}]; + tC.container.reload(o); + } catch (e) { + console.error(e); + } + }, 500 * k) + + } + }); + + + }); + }, + + track: function (type, page, extra) { + if (!this.fluidbook.support.hasNetwork() || this.fluidbook.nointerface) { + return; + } + + if (this.fluidbook.settings.stats) { + try { + this.trackFluidbook(type, page, extra); + } catch (e) { + + } + } + if (this.fluidbook.settings.statsMatomo) { + try { + this.trackMatomo(type, page, extra); + } catch (e) { + + } + } + try { + this.trackGoogleAnalytics(type, page, extra); + } catch (e) { + console.log(e); + } + }, + + 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; + } + + 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 = []; + } + }, + + trackMatomo(type, page, extra, url) { + if (url === undefined) { + url = window.location; + } + switch (type) { + case 0: // Change page + window._paq.push(['setCustomUrl', '/page/' + page]); + window._paq.push(['trackPageView', 'View page ' + page]); + break; + case 10: // Open Book + window._paq.push(['setCustomUrl', '/']); // Note: this shows as "/index" in the Matomo page URL reports (unless in flat view) + window._paq.push(['trackPageView', 'Open fluidbook']); + break; + case 14: // Open chapters menu + window._paq.push(['trackEvent', 'menu', 'click', 'chapters']); + break; + case 6: // Click a link in a page + window._paq.push(['trackEvent', 'link', 'click', extra]); + window._paq.push(['trackLink', extra]); + break; + case 1: // Search + window._paq.push(['trackSiteSearch', extra]); + break; + case 3: // Print + window._paq.push(['trackEvent', 'menu', 'click', 'print']); + break; + case 5: // Share + case 12: // Facebook + case 13: // Twitter + window._paq.push(['trackEvent', 'share', 'send', extra]); + break; + case 7: // PDF + case 8: // Partial PDF + window._paq.push(['trackEvent', 'menu', 'click', 'download']); + break; + case 4: // Bookmarks + window._paq.push(['trackEvent', 'bookmark', 'page', 'page ' + page]); + break; + case 9: // Extras + window._paq.push(['trackEvent', 'extra', 'click', extra]); + break; + case 2:// Zoom + window._paq.push(['trackEvent', 'zoom', 'page', page]); + break; + case 11:// Video + window._paq.push(['trackEvent', 'video', 'view', extra]); + break; + case 15:// + window._paq.push(['trackEvent', 'cart', 'addproduct', extra]); + } + }, + + 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 + this._ga('pageview', url); + break; + case 10: // Open Book + this._ga('pageview'); + break; + case 14: // Open chapters menu + this._ga('event', 'menu', 'click', 'chapters'); + break; + case 6: // Click a link in a page + this._ga('event', 'link', 'click', extra); + break; + case 1: // Search + var l = document.createElement('a'); + l.href = window.location.toString(); + if (l.search == '') { + l.search = '?'; + } else { + l.search += '&'; + } + l.search = 'q=' + extra; + this._ga('pageview', l.pathname + l.search + l.hash); + break; + case 3: // Print + this._ga('event', 'menu', 'click', 'print'); + break; + case 5: // Share + case 12: // Facebook + case 13: // Twitter + this._ga('event', 'share', 'send', extra); + break; + case 7: // PDF + case 8: // Partial PDF + this._ga('event', 'menu', 'click', 'download'); + break; + case 4: // Bookmarks + this._ga('event', 'bookmark', 'page', 'page ' + page); + break; + case 9: // Extras + this._ga('event', 'extra', 'click', extra); + break; + case 2:// Zoom + this._ga('event', 'zoom', 'page', page); + break; + case 11:// Video + this._ga('event', 'video', 'view', extra); + break; + case 15:// + this._ga('event', 'cart', 'addproduct', extra); + } + }, + + _ga: function (a0, a1, a2, a3, a4) { + console.log('_ga', a0, a1, a2, a3, a4); + var args = Array.prototype.slice.call(arguments); + if (this.ga === 'gtm') { + var dl = {}; + if (a0 === 'pageview') { + dl.event = 'pageview'; + if (a1 !== undefined) { + dl.location = a1; + } + } else if (a0 === 'event') { + dl.event = a1; + dl.eventLabel = a3; + dl.eventAction = a2; + } + dataLayer.push(dl); + } else if (this.ga === 'gtag') { + var f, o, a; + o = {}; + if (a0 === 'pageview') { + f = 'event'; + o = {}; + a = 'page_view'; + if (a1 !== undefined) { + o.page_path = a1; + } + } else if (a0 === 'event') { + f = 'event'; + a = a2; + o.event_category = a1; + o.event_label = a3; + } + + $.each(this.gaCodes, function (k, code) { + var data = $.extend({send_to: code}, o) + gtag(f, a, data); + }); + } else if (this.ga === 'ga') { + $.each(ga.getAll(), function (k, tracker) { + try { + tracker.send.apply(tracker, args); + } catch (e) { + } + }); + } + }, + + trackFluidbook: function (type, page, extra) { + if (page == undefined) { + page = 0; + } + if (extra == undefined) { + extra = ''; + } + + var $this = this; + + + var data = { + id: $this.id, vid: $this.vid, type: type, page: page, str: extra, time: new Date().getTime() + }; + + setTimeout(function () { + $this.fluidbook.networkControl.executeWhenNetwork(function (cb) { + $this.worker.postMessage(data); + setTimeout(cb, 100); + }, false); + }, 2500); + } +}; + -- 2.39.5