segment-integrations / analytics.js-integration-google-analytics

The Google Analytics analytics.js integration.
https://segment.com/docs/integrations/google-analytics/
MIT License
20 stars 23 forks source link

race condition on device mode... #81

Closed cristiannicola123 closed 6 years ago

cristiannicola123 commented 6 years ago

... a customer has reported events missing from their google analytics reports and after lots of time investigating this i have reached the point of finding a race condition.

by refreshing a page a few times (it takes on average about 50 or so clicks, so dont give up too quick) i can see that :

ga.loaded <true ga.getAll() <[]length: 0proto: Array(0)

normal expected network logs are: image

when the race condition happens

image

manually creating the ga with a ga.create('UA-xxxx-1', 'auto') will flush the analytics queue and events get send ok.

segment calls all are ok in either case, so i suspect the "loaded" function does not work as it was designed to...

cristiannicola123 commented 6 years ago

it looks like this is because some other library in the website does a window.ga = window.ga || function () { }

analytics code could probably check if the ga has been properly created & initialised instead of just check window.ga is assigned.