Skip to content

Instantly share code, notes, and snippets.

@JNaftali
Created August 13, 2025 17:32
Show Gist options
  • Select an option

  • Save JNaftali/c5a1f12baf6f8bca0c64dad766d823cd to your computer and use it in GitHub Desktop.

Select an option

Save JNaftali/c5a1f12baf6f8bca0c64dad766d823cd to your computer and use it in GitHub Desktop.
// This script is used to redirect users based on their geolocation and language preference.;
const userLanguageChoice = {
fr_ca: "fr-CA",
en_ca_prod: "eachandevery.com",
en_ca_stage: "each-every-staging-site.myshopify.com",
userLanguageChoice: "userLanguageChoice",
};
const consentMessage =
"<p style='margin:20px;'>This site is not accessible in your region. <br/><br/>Ce site n'est pas accessible dans votre région.</p>";
document.addEventListener("DOMContentLoaded", function () {
handleQuebecRedirection();
const onFrenchConsent = () => {
sessionStorage.setItem("userLanguageChoice", userLanguageChoice.fr_ca);
document.body.innerHTML = consentMessage;
};
function handleQuebecRedirection() {
const userChoice = sessionStorage.getItem(
userLanguageChoice.userLanguageChoice
);
const currentPath = window.location.pathname;
const curretHost = window.location.hostname;
const redirectToLanguage = () => {
document.body.innerHTML = consentMessage;
};
const handleGeoLocationSuccess = () => {
const location = window.OneTrust.getGeolocationData();
const isWithinRegion =
location.country === "CA" && location.state === "QC";
const isEnglishPath =
curretHost.includes(`${userLanguageChoice.en_ca_prod}`) ||
curretHost.includes(`${userLanguageChoice.en_ca_stage}`);
if (!userChoice && isEnglishPath && isWithinRegion) {
onFrenchConsent();
}
};
if (!currentPath.includes(`/${userLanguageChoice.fr_ca}`)) {
if (!userChoice) {
window.OptanonWrapper = handleGeoLocationSuccess;
} else if (
userChoice === userLanguageChoice.fr_ca &&
(curretHost.includes(`/${userLanguageChoice.en_ca_prod}`) ||
curretHost.includes(`/${userLanguageChoice.en_ca_stage}`))
) {
redirectToLanguage();
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment