Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dataLayer race condition #62

Closed
robflaherty opened this issue Jun 2, 2015 · 4 comments
Closed

dataLayer race condition #62

robflaherty opened this issue Jun 2, 2015 · 4 comments
Labels

Comments

@robflaherty
Copy link
Owner

JS error if dataLayer.push() is called before GA converts dataLayer from an array to a function.

@robflaherty robflaherty added the bug label Jun 2, 2015
@bmd
Copy link
Contributor

bmd commented Jun 3, 2015

dataLayer shouldn't be touched by GA - it's the array-like data object used by GTM. On a page without GTM included, typeof(dataLayer); -> "undefined"

You should be able use the standard dataLayer = dataLayer || [] pattern to initialize it and avoid a race condition on the push method if you need to push something onto it ahead of GTM being initialized but can't guarantee the order in which those things happen.

It seems like the real potential risk for a race condition is here since you aren't checking for the presence of the object you're firing an event to on the actual firing of the event. I think this might also cause a problem if you safe-initialized dataLayer as above and it had type "object" with a valid method push()

@robflaherty
Copy link
Owner Author

Hi,
Sorry, I left the bug description brief because this was just a reminder for myself. :)

The problem was this line: standardEventHandler = dataLayer.push; which works after GTM redefines dataLayer but not before when it's still an array. Simply wrapping it like this should fix:

standardEventHandler = function(data) {
  dataLayer.push(data);
}

Testing now and will push to master later today. Thanks.

@robflaherty
Copy link
Owner Author

btw, much of this needs to be rewritten. right now i just wanted to get a quick fix out. when i rewrite it i agree it'd be smart to make this more robust and check for the method presence before firing the event.

@bmd
Copy link
Contributor

bmd commented Jun 3, 2015

Cool - thanks again for maintaining this library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants