Last active
June 18, 2024 20:45
-
-
Save jamesmills/82bb21f6bf8b4f99b79cf538f4934dc8 to your computer and use it in GitHub Desktop.
save-url-parameters.js
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
/* | |
1. You'll need to have have the AnyTrack tracking code on your site for this to work. | |
2. Place the first script in GTM or in head of your HTML template. Load on every page. | |
3. Add the second script in the HTML page where you would like the LeadByte form to load. | |
4. Make sure to update the form ID and the URL to match. | |
*/ | |
<script> | |
var url = new URL(window.location.href); | |
var searchParams = url.searchParams; | |
// Check if there are any query parameters | |
if (searchParams.toString()) { | |
var paramsObject = {}; | |
searchParams.forEach(function(value, key) { | |
paramsObject[key] = decodeURIComponent(value); | |
}); | |
sessionStorage.setItem('urlParams', JSON.stringify(paramsObject)); | |
} | |
</script> | |
<script> | |
AnyTrack(function() { | |
// Extract atclid once AnyTrack script is loaded | |
var atclid = AnyTrack('atclid'); | |
// Retrieve the stored URL parameters from sessionStorage | |
var paramsObject = JSON.parse(sessionStorage.getItem('urlParams')) || {}; | |
// Append AnyTrack Click ID to the parameters object | |
paramsObject['atclid'] = atclid; | |
// Create a new URL object from the current window location | |
var url = new URL(window.location.href); | |
var search_params = url.searchParams; | |
// Append all parameters from the paramsObject to the search_params | |
for (var key in paramsObject) { | |
if (paramsObject.hasOwnProperty(key)) { | |
search_params.set(key, paramsObject[key]); | |
} | |
} | |
// Update the URL search part with the new search parameters | |
url.search = search_params.toString(); | |
// The new URL string | |
var new_url = url.toString(); | |
window.history.pushState({ path: new_url }, '', new_url); | |
// Load LeadByte form | |
var s = document.createElement('script'); | |
s.id = 'lb_form'; | |
s.type = 'text/javascript'; | |
s.async = true; | |
s.src = 'https://forms.leadbyte.co.uk/load.js?lbform52=241cf92bf5441b3af643195db7cb9f8f'; | |
document.body.appendChild(s); | |
}); | |
</script> | |
<div id="lbform52" style="width: 10px; min-width: 100%; height: 600px;"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment