Skip to content

Commit

Permalink
Merge pull request #172 from canonical/run-consent-script-by-default
Browse files Browse the repository at this point in the history
Run scripts instead of inserting into head
  • Loading branch information
samhotep authored Apr 4, 2024
2 parents f085d10 + 39d1f5e commit bdf7818
Showing 1 changed file with 15 additions and 49 deletions.
64 changes: 15 additions & 49 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,36 +102,16 @@ export const getControlsContent = (details, language) => {
};

export const addGoogleConsentMode = () => {
let consentAlreadySetup = false;

// Check for existing gtag, consentSetup before adding the default script
for (let item of document.scripts) {
if (item.innerHTML.includes("gtag") || item.innerHTML.includes("consent")) {
consentAlreadySetup = true;
}
}

if (!consentAlreadySetup) {
// Delete existing script
let oldScript = document.getElementById("google-consent-mode");
oldScript && oldScript.remove();

// Add to head section
const consentSetup = `
<script id="google-consent-mode">
// Define dataLayer and the gtag function.
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
// Set default consent to 'denied' as a placeholder
gtag('consent', 'default', ${JSON.stringify(DEFAULT_CONSENT)});
</script>`;

if (document.head.innerHTML) {
// Add to the top of the head section to ensure it's before the tag manager script
document.head.innerHTML = consentSetup + document.head.innerHTML;
} else {
document.head.innerHTML = consentSetup;
}
// Check for existing gtag before adding the default script
if (!window.gtag) {
// Run the script to define gtag
window.dataLayer = window.dataLayer || [];
window.gtag = function gtag() {
dataLayer.push(arguments);
};

// Set default consent to 'denied' as a placeholder
window.gtag("consent", "default", JSON.stringify(DEFAULT_CONSENT));
}
};

Expand All @@ -151,7 +131,7 @@ export const setGoogleConsentFromControls = (controls) => {
});

// Insert the script at the bottom of the head section
insertConsentScript(updatedConsent);
runConsentScript(updatedConsent);
};

export const setGoogleConsentPreferences = (selectedPreference) => {
Expand All @@ -161,7 +141,7 @@ export const setGoogleConsentPreferences = (selectedPreference) => {
);

// Insert the script at the bottom of the head section
insertConsentScript(updatedConsent);
runConsentScript(updatedConsent);
};

const updateConsentPreferences = (consentObject, selectedPreference) => {
Expand All @@ -188,23 +168,9 @@ const updateConsentPreferences = (consentObject, selectedPreference) => {
return updatedConsent;
};

const insertConsentScript = (consentObject) => {
// Delete existing script
let oldScript = document.getElementById("consent-mode-preferences");
oldScript && oldScript.remove();

let consentScript = `
<script id="consent-mode-preferences">
gtag("consent", "update", ${JSON.stringify(consentObject)})
</script>
`;

if (document.head.innerHTML) {
// Add to the bottom of the head section to ensure it's after the tag manager script
document.head.innerHTML = document.head.innerHTML + consentScript;
} else {
document.head.innerHTML = consentScript;
}
const runConsentScript = (consentObject) => {
// Update preferences
window.gtag("consent", "update", JSON.stringify(consentObject));
};

const pushPageview = () => {
Expand Down

0 comments on commit bdf7818

Please sign in to comment.