1const ROOT_WEB = 'https://laopcion.com.mx'; 2var openUrl = ''; 3 4self.addEventListener('push', function(event) { 5 console.log('Push message received', event); 6 7 var data = {}; 8 if (event.data) { 9 data = event.data.json(); 10 } 11 event.stopImmediatePropagation(); 12 showNotification(data); 13 14 sendAnalyticsEvent('show', data.notification.click_action); 15}); 16 17function showNotification(data) { 18 console.log('Push data received', data); 19 20 var notification = data.notification; 21 22 var click_action = notification.click_action; 23 console.log("click_action: "+click_action); 24 25 var icon_notification = ""; 26 icon_notification = notification.icon != null && notification.icon != '' ? notification.icon : ''; 27 28 var options = { 29 body: notification.body, 30 icon: icon_notification, 31 badge: icon_notification, 32 image: notification.image, 33 data: { 34 url: click_action + '?push=1' 35 } 36 }; 37 38 if (self.registration.showNotification) { 39 openUrl = click_action; 40 return self.registration.showNotification(notification.title, options); 41 } 42} 43 44self.addEventListener('notificationclick', function(event) { 45 console.log("openURL: "+openUrl); 46 var url = openUrl; 47 48 try { 49 url = event.notification.data.url; 50 } catch(err) { } 51 if (url == null || url == '') { 52 url = ROOT_WEB; 53 } 54 if (!event.action) { 55 sendAnalyticsEvent('click', openUrl); 56 // Was a normal notification click 57 event.waitUntil(self.clients.openWindow(url)); 58 event.notification.close(); 59 } 60}); 61 62// Set this to your tracking ID 63var trackingId = 'UA-155275194-2'; 64 65function sendAnalyticsEvent(eventAction, eventLabel) { 66 'use strict'; 67 68 console.log('Sending analytics event: ' + eventLabel + '/' + eventAction); 69 70 if (!trackingId) { 71 console.error('You need your tracking ID in analytics-helper.js'); 72 console.error('Add this code:\nvar trackingId = \'UA-XXXXXXXX-X\';'); 73 // We want this to be a safe method, so avoid throwing unless absolutely necessary. 74 return Promise.resolve(); 75 } 76 77 if (!eventAction && !eventLabel) { 78 console.warn('sendAnalyticsEvent() called with no eventAction or ' + 'eventLabel.'); 79 // We want this to be a safe method, so avoid throwing unless absolutely necessary. 80 return Promise.resolve(); 81 } 82 83 return self.registration.pushManager.getSubscription().then(function(subscription) { 84 if (subscription === null) { 85 throw new Error('No subscription currently available.'); 86 } 87 88 // Create hit data 89 var payloadData = { 90 // Version Number 91 v: 1, 92 // Client ID 93 cid: subscription.endpoint, 94 // Tracking ID 95 tid: trackingId, 96 // Hit Type 97 t: 'event', 98 // Event Category 99 ec: "PUSH", 100 // Event Action 101 ea: eventAction, 102 // Event Label 103 el: eventLabel 104 }; 105 106 // Format hit data into URI 107 var payloadString = Object.keys(payloadData) 108 .filter(function(analyticsKey) { 109 return payloadData[analyticsKey]; 110 }) 111 .map(function(analyticsKey) { 112 return analyticsKey + '=' + encodeURIComponent(payloadData[analyticsKey]); 113 }) 114 .join('&'); 115 116 // Post to Google Analytics endpoint 117 return fetch('https://www.google-analytics.com/collect', { 118 method: 'post', 119 body: payloadString 120 }); 121 }) 122 .then(function(response) { 123 if (!response.ok) { 124 return response.text() 125 .then(function(responseText) { 126 throw new Error( 127 'Bad response from Google Analytics:\n' + response.status 128 ); 129 }); 130 } else { 131 console.log(eventLabel + '/' + eventAction + ' hit sent, check the Analytics dashboard'); 132 } 133 }) 134 .catch(function(err) { 135 console.warn('Unable to send the analytics event', err); 136 }); 137}
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.