Skip to content

Instantly share code, notes, and snippets.

@kmclaugh
Created October 11, 2022 14:36

Revisions

  1. kmclaugh created this gist Oct 11, 2022.
    95 changes: 95 additions & 0 deletions atwave-shopify-thankyou.liquid
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    {% 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 %}