Skip to content

Instantly share code, notes, and snippets.

@elephantsneverforget
Created September 12, 2023 17:53
Show Gist options
  • Save elephantsneverforget/728f76c64fbddb85f9d72038d7a74b5a to your computer and use it in GitHub Desktop.
Save elephantsneverforget/728f76c64fbddb85f9d72038d7a74b5a to your computer and use it in GitHub Desktop.
<script id="elevar-dl-listener-config" type="application/json">{"data_layer_listener_script": "https://shopify-gtm-suite.getelevar.com/shops/c88f2922285b2da8178764fed5839861d5927386/3.4.3/events.js", "ss_url": "https://hits.getelevar.com", "signing_key": "UHcEMHgcma4ZgNIxXveQPEPocD6zZJj6", "myshopify_domain": "my-obvi.myshopify.com"}</script>
<script>
(() => {
if (!window.__ElevarIsGtmSuiteListenerCalled) {
window.__ElevarIsGtmSuiteListenerCalled = true;
const configElement = document.getElementById(
"elevar-dl-listener-config"
);
if (!configElement) {
console.error("Elevar: DLL Config element not found");
return;
}
const config = JSON.parse(configElement.textContent);
const script = document.createElement("script");
script.type = "text/javascript";
script.src = config.data_layer_listener_script;
script.async = false;
script.defer = true;
script.onerror = function () {
console.error("Elevar: DLL JS script failed to load");
};
script.onload = function () {
if (!window.ElevarGtmSuiteListener) {
console.error("Elevar: `ElevarGtmSuiteListener` is not defined");
return;
}
window.ElevarGtmSuiteListener.handlers.listen({
ssUrl: config.ss_url,
signingKey: config.signing_key,
myshopifyDomain: config.myshopify_domain
});
};
document.head.appendChild(script);
}
})();
</script>
<script id="elevar-dl-aat-config" type="application/json">{"data_layer_aat_script": "https://shopify-gtm-suite.getelevar.com/shops/c88f2922285b2da8178764fed5839861d5927386/3.4.3/aat.js", "apex_domain": "myobvi.com", "consent_enabled": false}</script>
<script>
(() => {
if (!window.__ElevarIsGtmSuiteAATCalled) {
window.__ElevarIsGtmSuiteAATCalled = true;
const init = () => {
window.__ElevarDataLayerQueue = [];
window.__ElevarListenerLoadQueue = [];
if (!window.dataLayer) window.dataLayer = [];
};
init();
window.__ElevarTransformItem = event => {
if (typeof window.ElevarTransformFn === "function") {
try {
const result = window.ElevarTransformFn(event);
if (typeof result === "object" && !Array.isArray(result) && result !== null) {
return result;
} else {
console.error("Elevar Data Layer: `window.ElevarTransformFn` returned a value " + "that wasn't an object, so we've treated things as if this " + "function wasn't defined.");
return event;
}
} catch (error) {
console.error("Elevar Data Layer: `window.ElevarTransformFn` threw an error, so " + "we've treated things as if this function wasn't defined. The " + "exact error is shown below.");
console.error(error);
return event;
}
} else {
return event;
}
};
window.ElevarPushToDataLayer = item => {
const enrichedItem = {
event_id: window.crypto.randomUUID ? window.crypto.randomUUID() : String(Math.random()).replace("0.", ""),
event_time: new Date().toISOString(),
...item
};
const transformedEnrichedItem = window.__ElevarTransformItem ? window.__ElevarTransformItem(enrichedItem) : enrichedItem;
const listenerPayload = {
raw: enrichedItem,
transformed: transformedEnrichedItem
};
const getListenerNotifyEvent = () => {
return new CustomEvent("elevar-listener-notify", {
detail: listenerPayload
});
};
if (transformedEnrichedItem._elevar_internal?.isElevarContextPush) {
window.__ElevarIsContextSet = true;
window.__ElevarDataLayerQueue.unshift(transformedEnrichedItem);
if (window.__ElevarIsListenerListening) {
window.dispatchEvent(getListenerNotifyEvent());
} else {
window.__ElevarListenerLoadQueue.unshift(listenerPayload);
}
} else {
window.__ElevarDataLayerQueue.push(transformedEnrichedItem);
if (window.__ElevarIsListenerListening) {
window.dispatchEvent(getListenerNotifyEvent());
} else {
window.__ElevarListenerLoadQueue.push(listenerPayload);
}
}
if (window.__ElevarIsContextSet) {
while (window.__ElevarDataLayerQueue.length > 0) {
window.dataLayer.push(window.__ElevarDataLayerQueue.shift());
}
}
};
const configElement = document.getElementById("elevar-dl-aat-config");
if (!configElement) {
console.error("Elevar: AAT Config element not found");
return;
}
const config = JSON.parse(configElement.textContent);
const script = document.createElement("script");
script.type = "text/javascript";
script.src = config.data_layer_aat_script;
script.async = false;
script.defer = true;
script.onerror = () => {
console.error("Elevar: AAT JS script failed to load");
};
script.onload = async () => {
if (!window.ElevarGtmSuiteAAT) {
console.error("Elevar: `ElevarGtmSuiteAAT` is not defined");
return;
}
window.ElevarGtmSuiteAAT.utils.emailCapture();
await window.ElevarGtmSuiteAAT.handlers.register({
apexDomain: config.apex_domain,
isConsentEnabled: config.consent_enabled
});
};
document.head.appendChild(script);
}
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment