|
| 1 | +/* global GA_ID, ga */ |
| 2 | + |
| 3 | +export default ({ router }) => { |
| 4 | +// Google analytics integration |
| 5 | + if (process.env.NODE_ENV === 'production' && GA_ID && typeof window !== 'undefined') { |
| 6 | + |
| 7 | + // snippet source: https://gist.github.com/DavidKuennen/443121e692175d6fc145e1efb0284ec9 |
| 8 | + (function (context, trackingId, options) { |
| 9 | + const history = context.history; |
| 10 | + const doc = document; |
| 11 | + const nav = navigator || {}; |
| 12 | + const storage = localStorage; |
| 13 | + const encode = encodeURIComponent; |
| 14 | + const pushState = history.pushState; |
| 15 | + const typeException = 'exception'; |
| 16 | + const generateId = () => Math.random().toString(36); |
| 17 | + const getId = () => { |
| 18 | + if (!storage.cid) { |
| 19 | + storage.cid = generateId() |
| 20 | + } |
| 21 | + return storage.cid; |
| 22 | + }; |
| 23 | + const serialize = (obj) => { |
| 24 | + var str = []; |
| 25 | + for (var p in obj) { |
| 26 | + if (obj.hasOwnProperty(p)) { |
| 27 | + if(obj[p] !== undefined) { |
| 28 | + str.push(encode(p) + "=" + encode(obj[p])); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + return str.join("&"); |
| 33 | + }; |
| 34 | + const track = ( |
| 35 | + type, |
| 36 | + eventCategory, |
| 37 | + eventAction, |
| 38 | + eventLabel, |
| 39 | + eventValue, |
| 40 | + exceptionDescription, |
| 41 | + exceptionFatal |
| 42 | + ) => { |
| 43 | + const url = 'https://www.google-analytics.com/collect'; |
| 44 | + const data = serialize({ |
| 45 | + v: '1', |
| 46 | + ds: 'web', |
| 47 | + aip: options.anonymizeIp ? 1 : undefined, |
| 48 | + tid: trackingId, |
| 49 | + cid: getId(), |
| 50 | + t: type || 'pageview', |
| 51 | + sd: options.colorDepth && screen.colorDepth ? `${screen.colorDepth}-bits` : undefined, |
| 52 | + dr: doc.referrer || undefined, |
| 53 | + dt: doc.title, |
| 54 | + dl: doc.location.origin + doc.location.pathname + doc.location.search, |
| 55 | + ul: options.language ? (nav.language || "").toLowerCase() : undefined, |
| 56 | + de: options.characterSet ? doc.characterSet : undefined, |
| 57 | + sr: options.screenSize ? `${(context.screen || {}).width}x${(context.screen || {}).height}` : undefined, |
| 58 | + vp: options.screenSize && context.visualViewport ? `${(context.visualViewport || {}).width}x${(context.visualViewport || {}).height}` : undefined, |
| 59 | + ec: eventCategory || undefined, |
| 60 | + ea: eventAction || undefined, |
| 61 | + el: eventLabel || undefined, |
| 62 | + ev: eventValue || undefined, |
| 63 | + exd: exceptionDescription || undefined, |
| 64 | + exf: typeof exceptionFatal !== 'undefined' && !!exceptionFatal === false ? 0 : undefined, |
| 65 | + }); |
| 66 | + |
| 67 | + if(nav.sendBeacon) { |
| 68 | + nav.sendBeacon(url, data) |
| 69 | + } else { |
| 70 | + var xhr = new XMLHttpRequest(); |
| 71 | + xhr.open("POST", url, true); |
| 72 | + xhr.send(data); |
| 73 | + } |
| 74 | + }; |
| 75 | + const trackEvent = (category, action, label, value) => track('event', category, action, label, value); |
| 76 | + const trackException = (description, fatal) => track(typeException, null, null, null, null, description, fatal); |
| 77 | + history.pushState = function (state) { |
| 78 | + if (typeof history.onpushstate == "function") { |
| 79 | + history.onpushstate({ state: state }); |
| 80 | + } |
| 81 | + setTimeout(track, options.delay || 10); |
| 82 | + return pushState.apply(history, arguments); |
| 83 | + } |
| 84 | + track(); |
| 85 | + context.ma = { |
| 86 | + trackEvent, |
| 87 | + trackException |
| 88 | + } |
| 89 | + })(window, GA_ID, { |
| 90 | + anonymizeIp: true, |
| 91 | + colorDepth: true, |
| 92 | + characterSet: true, |
| 93 | + screenSize: true, |
| 94 | + language: true |
| 95 | + }); |
| 96 | + |
| 97 | + router.afterEach(function (to) { |
| 98 | + ga('set', 'page', to.fullPath) |
| 99 | + ga('send', 'pageview') |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
| 103 | + |
0 commit comments