Created
October 11, 2022 14:36
-
-
Save kmclaugh/aff8504ba8efdba65b88d568b9895bf0 to your computer and use it in GitHub Desktop.
Atwave purcahse pixel on Shopify confirmation page additional scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if first_time_accessed %} | |
<script> | |
// Require the necessary APIs | |
var logToConsole = console.log; | |
var log = logToConsole; | |
var localStorage = window.localStorage; | |
getCookie = function (name) { | |
var match = document.cookie.match(new RegExp("(^| )" + name + "=([^;]+)")); | |
if (match) return match[2]; | |
else return []; | |
}; | |
injectScript = function (src, onSuccess, onFailure) { | |
var script = document.createElement("script"); | |
log(src); | |
script.setAttribute("src", src); | |
script.setAttribute("type", "text/javascript"); | |
script.onload = function handleScriptLoaded() { | |
onSuccess(); | |
}; | |
script.onerror = function handleScriptError() { | |
onFailure(); | |
}; | |
document.head.appendChild(script); | |
}; | |
// Get the user data | |
var offerId = "2130"; | |
var eventId = "339"; | |
var trackingType = "conversion"; | |
var transactionId = "{{ order.id }}"; | |
// Set session and transaction id keys | |
var sessionIdKey = "__request_session_id_at_wave"; | |
// Get the requestSessionID | |
var requestSessionId; | |
// Get session Ids if available | |
var sessionLocalStorageID = localStorage.getItem(sessionIdKey); | |
var sessionCookieID = getCookie(sessionIdKey); | |
sessionCookieID = sessionCookieID.length > 0 ? sessionCookieID[0] : null; | |
requestSessionId = sessionLocalStorageID || sessionCookieID; | |
var idFrom = sessionLocalStorageID ? "local storage" : "cookies"; | |
if (requestSessionId) { | |
var message = | |
"Atwave: found request session id " + requestSessionId + " in " + idFrom; | |
log(message); | |
} else { | |
log("AtWave: no session id found in local storage or cookies"); | |
} | |
if (requestSessionId) { | |
// Generate the url | |
var url = | |
"https://scalebus.com/p.ashx?o=" + | |
encodeURIComponent(offerId) + | |
"&r=" + | |
encodeURIComponent(requestSessionId) + | |
"&f=js"; | |
if (eventId) { | |
url = url + "&e=" + encodeURIComponent(eventId); | |
} | |
if (transactionId) { | |
url = url + "&t=" + encodeURIComponent(transactionId); | |
} | |
// If the script loaded successfully, log a message and signal success | |
var onSuccess = () => { | |
log("AtWave: Script loaded successfully."); | |
// Write the transaction Id to local stores and cookie if desired | |
}; | |
// If the script fails to load, log a message and signal failure | |
var onFailure = () => { | |
log("AtWave: Script load failed."); | |
}; | |
// If the URL input by the user matches the permissions set for the template, | |
// inject the script with the onSuccess and onFailure methods as callbacks. | |
if (trackingType === "conversion") { | |
log("Atwave: Loading script from " + url); | |
injectScript(url, onSuccess, onFailure); | |
} else { | |
log("Atwave: pageview event. No script fired."); | |
} | |
} | |
</script> | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment