feat(ga): transport for event tracking #371
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
GA provider for analytics.js implementations has a (what appears to be long standing) bug that doesn't correctly pass the "transport" option to GA.
When transport is set to "beacon" the resulting hit is still sent using an image by default.
When transport is set to "beacon" eventTrack() now correctly uses analytics.js' support for navigator.sendBeacon().
The core of the problem seems to be an incorrect use of the GA API.
The ga.send() function only accepts a single fieldsObject: https://developers.google.com/analytics/devguides/collection/analyticsjs/tracker-object-reference#send
Demonstration of the underlying issue: https://gist.githack.com/goosmurf/9fec28db0e9747baa70a0120af59e389/raw/f2a18b1cdde9bcfb33fb272c05d5fcaa1cbc95a7/mixed-transport.html
Watch the network tab in dev tools and you'll see that the "Broken beacon" is sent using an image, whereas the "Working beacon" is sent via navigator.sendBeacon (identifiable by both the HTTP method being POST as well as the Initiator on the request).