Created
May 3, 2023 21:35
-
-
Save appikonda/7bad81cb35dd96d2f9530457445baf9d to your computer and use it in GitHub Desktop.
Local Storage
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
function utmLocalstorageTag() { | |
const queryString = window.location.search; // url query string | |
const urlParams = new URLSearchParams(queryString); // get parameters in url | |
// get required parameters for the LocalStorage management | |
const utm_medium_value = urlParams.get('utm_medium'); | |
if (utm_medium_value) { | |
const previousMediumValue = getItem("_utmMedium"); // check for previous UTM medium value in LocalStorage | |
if (previousMediumValue) { | |
const updated_utm_medium_value = previousMediumValue + ":" + utm_medium_value; // concat new utm_medium value to prev LocalStarage | |
setItem("_utmMedium", updated_utm_medium_value); | |
} else { | |
setItem("_utmMedium", utm_medium_value); // create new _utmMedium LocalStarage | |
} | |
} | |
} | |
// create LocalStarage | |
function setItem(lname, lvalue) { | |
localStorage.setItem(lname, lvalue); | |
} | |
// retrieve LocalStarage | |
function getItem(lname) { | |
if (lname) { | |
return localStorage.getItem(lname); | |
} | |
return ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment