In Belgium, a lot of websites have content in two or more languages. If we want to aggregate data in Google Analytics based on the language of the content our visitors viewed, we need to have some way of detecting that. We can solve this by creating a custom variable in Google Tag Manager that contains the language of the page the visitor is currently viewing and send that as a custom dimension to Google Analytics.


Our goal is to create a custom variable that contains the language of the page that is currently viewed. On our website, semetis.com, all pages are by default in French. If you change the language, /nl/ or /en/ is added at the beginning of the web page url. This can be easily detected with JavaScript so we can create the variable as a Custom JavaScript variable.


/// the languages to detect, default to the first one
var languages = ['fr', 'en', 'nl'];

/// test the url and return the language
for(var l =0; l < languages.length; l ++)
if(window.location.href.indexOf('/' + languages[l] + '/') > -1)
return languages[l];

/// language not found, return the default one
return languages[0];


Next we need to define the custom dimension in Google Analytics.



Finally we need to create the Google Analytics tag that sends the Page Language as a custom dimension.


The tag is created


You’re all done. Don’t forget to publish the changes. You should now be receiving the page language in Google Analytics.

Author: David Peeters