1'use strict'; 2 3/* eslint-env browser, serviceworker */ 4// Make use of Google Analytics Measurement Protocol. 5// https://developers.google.com/analytics/devguides/collection/protocol/v1/reference 6class Analytics { 7 trackEvent(eventCategory, eventAction) { 8 if (!this.trackingId) { 9 console.error('You need to set a trackingId, for example:'); 10 console.error('self.analytics.trackingId = \'UA-XXXXXXXX-X\';'); 11 12 // We want this to be a safe method, so avoid throwing Unless 13 // It's absolutely necessary. 14 return Promise.resolve(); 15 } 16 17 if (!eventCategory && !eventAction) { 18 console.warn('sendAnalyticsEvent() called with no eventCategory or ' + 19 'eventAction.'); 20 return Promise.resolve(); 21 } 22 23 return self.registration.pushManager.getSubscription() 24 .then(subscription => { 25 if (subscription === null) { 26 // The user has not subscribed yet. 27 throw new Error('No subscription currently available.'); 28 } 29 30 const payloadData = { 31 // Version Number 32 v: 1, 33 // Client ID 34 cid: subscription.endpoint, 35 // Tracking ID 36 tid: this.trackingId, 37 // Hit Type 38 t: 'event', 39 // Data Source 40 ds: 'serviceworker', 41 // Event Category 42 ec: eventCategory, 43 // Event Action 44 ea: eventAction, 45 ni: 1 46 }; 47 48 const payloadString = Object.keys(payloadData) 49 .filter(analyticsKey => { 50 return payloadData[analyticsKey]; 51 }) 52 .map(analyticsKey => { 53 return `${analyticsKey}=` + 54 encodeURIComponent(payloadData[analyticsKey]); 55 }) 56 .join('&'); 57 58 return fetch('https://www.google-analytics.com/collect', { 59 method: 'post', 60 body: payloadString 61 }); 62 }) 63 .then(response => { 64 if (!response.ok) { 65 return response.text() 66 .then(responseText => { 67 throw new Error( 68 `Bad response from Google Analytics ` + 69 `[${response.status}] ${responseText}`); 70 }); 71 } 72 }) 73 .catch(err => { 74 console.warn('Unable to send the analytics event', err); 75 }); 76 } 77} 78 79if (typeof self !== 'undefined') { 80 self.analytics = new Analytics(); 81}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.