Skip to content

Instantly share code, notes, and snippets.

@appikonda
Created May 3, 2023 21:35
Show Gist options
  • Save appikonda/7bad81cb35dd96d2f9530457445baf9d to your computer and use it in GitHub Desktop.
Save appikonda/7bad81cb35dd96d2f9530457445baf9d to your computer and use it in GitHub Desktop.
Local Storage
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