Created
November 13, 2017 23:23
-
-
Save DHS/2f2ed5ede51e973891718b9f70333229 to your computer and use it in GitHub Desktop.
Extract a url parameter and pass it into a tracking pixel url
This file contains hidden or 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
// This script extracts a url parameter (transactionId) and passes it into a tracking pixel url (trackingPixelUrl) | |
// It is designed to be added to a squarespace custom js block | |
// | |
// References: | |
// https://github.com/moonclerk/developer/blob/master/integration.md | |
// https://answers.squarespace.com/questions/78841/can-a-text-field-be-pre-filled-based-on-a-url-parameter.html | |
window.onload = function() { | |
// Create a function that fetches a given argument from the url | |
function fetchUrlArgument(argumentName) { | |
// Build array of url arguments | |
queryString = window.location.search.substring(1); | |
arguments = queryString.split("&"); | |
// Loop through all the arguments | |
for (i = 0; i < arguments.length; i++) { | |
// Split each argument (key=value) on the equals sign | |
argument = arguments[i].split("="); | |
// Check if the key matches the argument we're looking for | |
if (argument[0] == argumentName) { | |
// If it does then return the value | |
return argument[1]; | |
} | |
} | |
} | |
// Call the function for the argument we want to fetch | |
var transactionId = fetchUrlArgument("transactionId"); | |
// If the argument is found then display our tracking pixel | |
if (transactionId) { | |
// Setup the tracking pixel | |
trackingPixelUrl = 'https://google.com/?id=' | |
trackingPixel = '<iframe src="' + trackingPixelUrl + transactionId + '" height="1" width="1" frameborder="0"></iframe>'; | |
// Find the tracking pixel placeholder | |
trackingPixelPlaceholder = document.getElementById("trackingPixelPlaceholder"); | |
// If the placeholder exists then inject the tracking pixel | |
if (trackingPixelPlaceholder) { | |
trackingPixelPlaceholder.innerHTML = trackingPixel; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment