angulartics / angulartics2

Vendor-agnostic analytics for Angular2 applications.
MIT License
1.01k stars 193 forks source link

feat(ga): transport for event tracking #371

Closed goosmurf closed 4 years ago

goosmurf commented 4 years ago

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).

codecov-io commented 4 years ago

Codecov Report

Merging #371 into master will increase coverage by 0.09%. The diff coverage is 50%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #371      +/-   ##
==========================================
+ Coverage   75.06%   75.15%   +0.09%     
==========================================
  Files          32       32              
  Lines         818      817       -1     
  Branches      149      149              
==========================================
  Hits          614      614              
+ Misses        135      134       -1     
  Partials       69       69
Impacted Files Coverage Δ
src/lib/providers/ga/ga.ts 64.77% <50%> (+0.72%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update cf90e8f...7bf0bfe. Read the comment docs.

scttcper commented 4 years ago

:tada: This PR is included in version 8.3.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: