Create "tracking snippet" to avoid race condition with async script loading #119
justusbluemer
started this conversation in
Ideas
Replies: 2 comments
-
We fixed the documentation and it should be easier to understand now. I added this note // Or define the elb function manually in the browser as an alternative
function elb(){(window.elbLayer = window.elbLayer || []).push(arguments);} The walker comes with async support but the docs were wrong. Thank you @justusbluemer for the detailed description. Hope it's better now! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
if I understand the documentation correctly, one of the options to use walker.js includes calling either
elb()
(https://docs.elbwalker.com/getting-started/commands) orelbwalker.push
(https://docs.elbwalker.com/tagging/user-identification)This probably works fine when importing walker.js as part of a build process à la
import { elb } from '@elbwalker/walker.js';
, but fails regularly when adding the script to a site with anasync
attribute, as described at the bottom of https://docs.elbwalker.com/getting-started/installation:<script async class="elbwalker" src="https://cdn.jsdelivr.net/npm/@elbwalker/walker.js@latest/dist/walker.js" data-default="true"></script>
When the script is loaded asynchronously but elb() or elbwalker.push() executed immediately afterwards, they might not be defined yet and the call will fail à la
Uncaught ReferenceError: elbwalker is not defined
.Google Tag Manager and other libraries circumvent this by providing a standardized "tracking snippet" that defines dataLayer (GTM) or fbq (Meta Pixel) directly in the snippet and processes any data pushed through them once the main script is loaded.
Judging from experience, adding a tracking code snippet "manually" to a page template is still the preferred way of loading scripts, even (sometimes especially) with large enterprise companies. Maybe that's an opportunity to make the existing installation instructions even easier.
(btw the CDN snippet at the bottom of the documentation doesn't work out of the box; "LATEST" in the URL should be a lowercase "latest")
Beta Was this translation helpful? Give feedback.
All reactions