Skip to main content
All CollectionsAnalytics
Dynamically Switching Google Tag Manager containers
Dynamically Switching Google Tag Manager containers

This advanced topic covers situations where you need to modify the GTM container invoked by MetaLocator dynamically

Michael Fatica avatar
Written by Michael Fatica
Updated this week

MetaLocator supports Google Tag Manager installation by simply copying and pasting the Google Tag Manager container ID into the setting shown below:

However, what happens if you want to switch the Google Tag Manager container ID dynamically? For example, you might have one interface used for multiple countries, which require different Google Tag Manager container IDs.

One solution would be to duplicate your interfaces and manually update the Google Tag Manager code in each Interface, but that would be hard to manage for scores or hundreds of countries. In general, it is considered best practice to avoid duplicating Interfaces solely for the sake of per-country or per-language differences.

The solution is to dynamically change settings using MetaLocator's deep linking options. These allow you to change settings in the installation code when the page loads.

For example, changing the active language is commonly accomplished as shown below:

<script>
// this signals to MetaLocator to load the German language file
var ml___params = {"lang":"de-DE (1)"};
</script>

<!-- MetaLocator.com Code START --><script language="javascript" src="//code.metalocator.com/index.php?option=com_locator&view=directory&layout=_javascript&framed=1&format=raw&tmpl=component&no_html=1&Itemid=XXXXXXXXX"></script><!-- MetaLocator.com Code END: Do not modify this code block. See TOS for details. -->

The first script tag is where the customization occurs. The second script tag is the un-modified MetaLocator-provided installation code. Of course, your code will include the actual interface ID instead of "XXXXXX"

To update the Google Tag Manager container ID, the solution is a bit different since we're modifying an interface setting. In the below example, we're changing both the language and the GTM container id.

<script>
// this signals to MetaLocator to load the German language file
var ml___params = {"lang":"de-DE (1)"};

// this signals to MetaLocator invoke the GTM Container "GTM-123456ABC"
var ml___options = {"google_tag_manager":"GTM-123456ABC"};

</script>

<!-- MetaLocator.com Code START --><script language="javascript" src="//code.metalocator.com/index.php?option=com_locator&view=directory&layout=_javascript&framed=1&format=raw&tmpl=component&no_html=1&Itemid=XXXXXXXXX"></script><!-- MetaLocator.com Code END: Do not modify this code block. See TOS for details. -->

For more information on deep linking options and parameters, see the deep linking article.

Did this answer your question?