Browser-tracker not working in a Chrome Extension

This is how I am initializing my tracker inside my chrome extension using @snowplow/browser-tracker

    newTracker('sp', 'fd38d013-9bb8-4332-aef9-f86514581918.app.try-snowplow.com', { 
      appId: 'myApp', 
      respectDoNotTrack: false,
      platform: 'web',
      contexts: {
        webPage: true,
        performanceTiming: true,
      },
      post: true,
      bufferSize: 1
    })
  }

And this is the event I am sending.

    trackStructEvent({
      category: 'trackStructEvent',
      action: 'event',
      label: name,
      property: md5(trackingUserId),
      value: 0.0
    })

But I get no events sent, It is almost like is not firing. I see using break points that the code is being hit. Is there any consideration I am missing considering this code is running inside of a service worker of a Chrome Extension V3. Thanks for your help

I will take a look. Thanks

I don’t think the browser tracker works in ServiceWorker environments; these don’t usually have XMLHttpRequest defined, and the logic for sending events in that scenario skips straight to new Image() as a fall back, and that’s also probably not supported since there’s no DOM. (I’m not sure if navigator.sendBeacon() would work, but the current implementation doesn’t even try if XMLHttpRequest isn’t detected).

Ideally browser-tracker-core would try to use fetch and fall back to XMLHttpRequest or try sendBeacon independently but until then you might be stuck with using tracker-core directly and using your own “emitter” built using the fetch() API, or forking browser-tracker & browser-tracker-core to adjust the logic to untangle the assumption that sendBeacon wouldn’t exist without XMLHttpRequest.

I see. This makes a lot of sense. I’ll see if I can use browser-tracker core and I’ll update this if I succeed

@jethron you were right. So I ended up moving my analytics to the scripts that handle my popups and I am good now. Thanks for your help!

1 Like