analytics
analytics copied to clipboard
sendBeacon support
Some tools such as GA support a transport=beacon option that tells it to use navigator.sendBeacon to send the request. See an example for GA's analytics.js here.
Do you have any plans to support this as a param to pass through, or perhaps automatically use the beacon-based approach when its supported? It's important for firing any events before unload or a redirect without needing workarounds such as a delay via setTimeout. Apologies if I just missed any documentation around this.
Thanks!
Having a beacon option would be cool. I was exploring this with the aws-pinpoint plugin https://github.com/DavidWells/analytics/blob/master/packages/analytics-plugin-aws-pinpoint/src/pinpoint/index.js#L88
It's tricky to make this work across the board for all plugins though. Each provider has it's own special lil' thing they need or they don't support raw HTTP calls etc.
Perhaps a @analytics/beacon-utils utility that plugins can use might make sense.
What do you think?
Also, do you know how the GA beacon call is being made? Is it a call to their gif endpoint or to another URL?
According to the comment in the code of the google page I linked to in my first post it's using navigator.sendBeacon...quickly trying it out in the browser it looks like it posts to https://www.google-analytics.com/collect.
Their approach seems to be allowing you to specify it via 'transport': 'beacon', and they will use that transport mechanism if it is supported by the browser but fall back otherwise. gtag.js sounds like it does the same from other docs I came across.
I think it would be nice to either have more control over the GA plugin so transport could be passed, or have a similar transport arg for your library's track calls -- it would just try to use a beacon if the plugin supports it. Having separate beacon-utils may be a nice addition either way.
Aha maybe we can add as a setting to google analytics plugin https://developers.google.com/analytics/devguides/collection/analyticsjs/sending-hits
Looks like we need something like:
// Updates the tracker to use `navigator.sendBeacon` if available.
if (config.useBeacon) {
ga('set', 'transport', 'beacon');
}
Inside initialize https://github.com/DavidWells/analytics/blob/master/packages/analytics-plugin-google-analytics/src/browser.js#L97