In Google Tag Manager, the language of the Cookiebot consent banner can be controlled by a GTM variable. This allows you to automatically detect which language your banner should be displayed in.
We assume that you've already implemented the Cookiebot CMP tag in GTM.
While this can be achieved in multiple ways (you may for example create a Custom JavaScript Variable named "Current Language" that returns a two-letter ISO language code based on custom logic), in this example the language is determined by a sub-directory.
For example: domain.com/xx/, domain.com/xx-xx/, or domain.com/xx_xx/
If no match is found for such a sub-directory, a sub-domain will be used instead.
For example: xx.domain.com.
If neither of these gave us a language code, the domain extension will be used.
For example: domain.xx
If no language sub domain/directory is available and the the domain extension is more than 3 characters long (i.e. com, net, or info) and none of the above yielded a language code, then your default language will be applied.
The script also takes into account cases where the country and language ISO codes don't match, as is for example the case for Estonia (EE) : Estonian (ET).
Implementation
- Create a new Custom JavaScript Page Variable
- Assign the new variable a name, for example: Language from URL
- Add the following code to the text field:
function () { var d = "en", //Set your default language here l = { at:"de",au:"en",ca:"en",cz:"cs",dk:"da",ee:"et",no:"nb",se:"sv",uk:"en" }, h = window.location.hostname.toLowerCase(), p = window.location.pathname.toLowerCase(), r = [ /^\/([a-z]{2})([_-][a-z]{2})?(\/.*)?$/g.exec(p), /^([a-z]{2})\./.exec(h), /\.([a-z]{2,})$/.exec(h) ], c = ""; for (var i = 0; i < r.length; i++) { c = r[i] ? r[i][1] : d; if (r[i]) break; } c = c.length != 2 ? d : c; if (l[c]) c = l[c]; return c; }
Once saved, add the variable to your Cookiebot CMP tag in GTM by selecting "By GTM Variable" from the "Language" selection field, and selecting the {{Language from URL}}
variable in the "Language Variable" selection field.
Comments
0 comments
Please sign in to leave a comment.