-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Translations): Add translations analytics (#1881)
* feat(Translations): Add translations analytics * added support for Edge, and language detection where possible * Added whitespace for clarity * Get the language from the mutation target * Combine the translate log and language * Added support for new google tag manager environments * Updated comment
- Loading branch information
Showing
5 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Detects if the page is translated and notifies analyticis | ||
|
||
// adapted from: https://analytical42.com/2022/detect-track-translations-ga4/ | ||
// This code does its best to detect if the user changes the language of the page. | ||
// Right now it fully supports logging from users changing the page language from the website itself regardless of the browser used | ||
// This struggles with detecting and reporting the new language when the user uses the browser to | ||
// change the language. | ||
// Chrome - Full support, we can detect when the user translates the page, and what language the page is translated to | ||
// Safari - Full support, we can detect when the user translates the page, and what language the page is translated to | ||
// Edge - Half support, we can detect when the user translates the page, just not what language they changed it to | ||
// Firefox - Full Suuport, we can detect when the user translates the page and what language the page is translated to | ||
export default () => { | ||
// Start by checking if the MutationObserver API is available | ||
if (typeof MutationObserver === "function") { | ||
// Tell the observer to monitor for changes to HTML attributes | ||
const config = { attributes: true }; | ||
// Build the function to run when a change is observed | ||
const callback = mutationList => { | ||
// Loop through each observed change | ||
for (let i = 0; i < mutationList.length; i += 1) { | ||
// Only do something if the change was on an attribute | ||
if (mutationList[i].type === "attributes") { | ||
if ( | ||
// Check for Edge's browser translation attributes | ||
// Edge adds 2 attributes (_msttexthash and _msthash), so we only check for one to avoid duplicate logging | ||
mutationList[i].attributeName === "_msttexthash" || | ||
// Check for Chrome, Google, and Safari's browser translation lang attribute | ||
mutationList[i].attributeName === "lang" | ||
) { | ||
// Grab the new language from the html lang attribute (if available) | ||
const newLanguage = mutationList[i].target.getAttribute("lang"); | ||
window.dataLayer = window.dataLayer || []; | ||
// Send an event to the dataLayer | ||
// Only log if we can detect the new language and it isn't english (the default of the page which some browsers do not update) | ||
window.dataLayer.push({ | ||
event: "translate", | ||
language: newLanguage && newLanguage !== "en" ? newLanguage : "" | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
// Create the actual observer | ||
const observer = new MutationObserver(callback); | ||
// Attach the observer to the <title> tag | ||
observer.observe(document.getElementsByTagName("title")[0], config); | ||
// Attach the observer to the <html> tag | ||
observer.observe(document.getElementsByTagName("html")[0], config); | ||
} | ||
}; |
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
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
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