Objective
Let’s say you’re a website operating in Australia but have incidental traffic from other countries. If a visitor is in the European Economic Area they would be subject to GDPR, meaning you might want to change how you track them (if at all). There are plenty of Consent Management Platforms out there which can integrate with Google Tag Manager and set up different consent notices for different locations, however for a smaller business this might be overkill. If your target market is, say, in Australia, could you just fire tracking codes like the Meta Pixel in Australia only? This would save a lot of privacy/compliance hassle.
Results
Luckily you can do this in Google Tag Manager alone without a subscription. Here’s a slightly simplified recipe based on a range of implementations on several client websites.
Step 1: Geolocate the user and save their location
By default, the user’s country is only available server-side, not in GTM, which is a client-side tool. There are, however, some services you can ping from the user’s browser using Javascript. Some of them are even free and unlimited, for example this one. If you go to https://geolocation-db.com/jsonp/, you will get back this database’s best guess of where you are, so if you call it from GTM it will be the location of the person browsing your website. It’s not perfectly accurate but neither is any geolocation service.
Now once we know which country a user is in, we should save this value in the user’s browser using local storage so that we don’t keep pinging the service for every page a user visits. Actually we just have to save a single flag: is a user in a country where we will fire the Meta Pixel (Australia) or not (any other country)?
See the code below, which would then be the first tag that fires, way before the Meta Pixel tag. Note that you will need to wrap this code in <script> tags. Click to copy the code.
// This geolocation service calls a function called "callback" when done so we should define it
function callback(data){
var countriesWithAdPixels = /AU|NZ/; // Edit this regex as the whitelist
var areWeFiringAdPixels = data.country_code.match(countriesWithAdPixels)!==null;
setUpTracking(areWeFiringAdPixels,true); // Fire the pixel and save the result
};
// This function fires before every ad pixel pageview tag.
function setUpTracking(areWeFiringAdPixels,saveResult) {
// This function might run because the geolocation tool returned an answer, or because it timed out.
// Either way, we don't fire this more than once
if(!window.trackingAlreadySetup){
window.trackingAlreadySetup = true;
//Fire the relevant ad pixel pageview tags only if we are firing the ad pixels.
if(areWeFiringAdPixels){
dataLayer.push({
'event':'fire_ad_page_tags'
});
}
// If the geolocation did NOT time out, save it in local storage
// don't need to geolocate the user again
if(saveResult){
try{
window.localStorage.setItem("areWeFiringAdPixels", areWeFiringAdPixels);
}catch(e){}
}
}
}
// The main code.
try{
var areWeFiringAdPixels = window.localStorage.getItem("areWeFiringAdPixels");
}catch(e){}
// Only run the geolocation if the value is not previously set in local storage.
if(!areWeFiringAdPixels) {
// If there is no geolocation of the user within 2.5 seconds, we do NOT fire ad pixels (to be
// on the safe side) but also we do NOT save the result in a cookie. This means if the user views
// Another page, we will try to geolocate again.
setTimeout(function(){setUpTracking(false,false);},2500);
//Geolocate the user by creating a script from https://geolocation-db.com/jsonp.
var geolocationScript = document.createElement('script');
geolocationScript.src = "https://geolocation-db.com/jsonp";
document.body.appendChild(geolocationScript);
}
//Otherwise we just retrieve the value from saved cookie and if it's true, we fire the ad pixels
else if(areWeFiringAdPixels == true || areWeFiringAdPixels == "true"){
dataLayer.push({
'event':'fire_ad_page_tags'
});
}
Step 2: Create a custom trigger for the ad pixel
We then create a custom trigger to match when the above tag sends an event called “fire_ad_page_tags” into the data layer.
Step 3: Attach the ad pixel tag to this event
Finally, in our Meta Pixel tag, we change the trigger from something like “All Pages” to the new one we created.
That’s it! You can modify the above to create more granular options depending on how international your traffic is.
