Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding analytics.js and fbevents.js from Service Worker

Is there a possibility to exclude fetching Google Analytics, Google Tag Manager and Facebook Pixel scripts via Service Worker in PWA?

From what I found out today - Facebook Pixel Helper and Google Tag Assistant return errors after reload if a website uses Service Workers. I'm not entierly sure if the data is not sent though (Chrome Dev Tools show that request to Facebook and Google is beeing sent with code 200).

It looks like both scripts fire on the first visit but won't fire on reload. I have no programming experience and have a vague notion of how Service Workers work but I need to make sure that I get all the data correctly because I'm an analyst. Can anyone point me to the right direction?

like image 796
th4 Avatar asked Nov 06 '22 21:11

th4


1 Answers

If you have problem with facebook pixel, you can try this way. You should exclude facebook request in your service worker.

self.addEventListener('fetch', function(event) {

  if(event.request.url.search("www.facebook.com") != -1 || event.request.url.search("connect.facebook.net") != -1){
    // hack FB Pixel
  }else{
    event.respondWith(
        fetch(event.request).catch(function(error) {
          console.log( '[PWA Builder] Network request Failed. Serving content from cache: ' + error );

          //Check to see if you have it in the cache
          //Return response
          //If not in the cache, then return error page
          return caches.open('pwabuilder-offline').then(function (cache) {
            return cache.match(event.request).then(function (matching) {
              var report =  !matching || matching.status == 404?Promise.reject('no-match'): matching;
              return report
            });
          });
        })
    );
  }



});

In the same way you can hack Google Tag Assistant

like image 166
DevQuayle Avatar answered Nov 13 '22 19:11

DevQuayle