Quick question, where are you from?
Well, that's probably a question that a website has never asked you specifically.
But they know exactly where you are at because they give you suggestions or advertising that
is actually tailored to your location.
So how do they do this well it's something called Geolocation.
And today we're gonna find out how to install something like this with the help of Google
Tag Manager.
All and more, coming up!
Hey there welcome back to another video of measure school.com teaching you the data-driven
way of digital marketing.
If you're new here then hit that subscribe button and also click that bell notification
icon to stay up to date with what we do on this channel.
Now Geolocation has been around for a long time in digital marketing specifically because
you probably have seen advertising like singles in your area, or the language settings or
the currency settings were automatically adjusted on your location.
Then this is the work of Geolocation.
Now you're probably wondering how can I utilize Geolocation in combination with Google Tag
Manager to for example deploy codes or show certain things on the website based on the
location of the user.
Well, it's actually not a built-in functionality of Google Tag Manager per se and that's why
we need to utilize third-party services to get that Geolocation information.
And in this tutorial, I'm gonna show you how.
So let's dive in.
Before we get started, let's acknowledge first that with the means of Google Tag Manager
it's not possible to locate the user.
Why is that?
Well, you would need to have the IP address of the request that comes in from the server
to geolocate the user.
Now Google Tag Manager is basically JavaScript it runs on the browser of the user and there's
no way of identifying the IP address through the browser alone.
And therefore there's also no way in Google Tag Manager to for example build a variable
that would get us the IP address through JavaScript.
The only way we could do this is through outside help from either your server where you could
install a script that sends it back to the page, or use a Geolocation API.
One of those is geoPlugin.
It's a free API service that you can install on your website.
There are some important limitations to this geoPlugin and I recommend to read through
their user’s policy if you are eligible for the free tier here.
You don't have to sign up to anything but if you want to use the geoPlugin with your
SSL site so you have HTTPS here in the front, then you need to pay for access which is quite
affordable I think it's $12 per year.
For that you would then also need your API key to attach to the script here.
So how would we install this?
All you would need is this JavaScript here that you can simply copy from their website
and we'll install this with Google Tag Manager.
So we'll just open up a custom HTML tag call this geo for now and implement your code.
Then let's attach a trigger I'll just go with the old pages trigger, save this.
Refresh our preview and debug mode, refresh our page, and this is now deployed on our
website.
Now what it does it requests information from the geoPlugin that geoPlugin will send the
data back and give us some information informs of some JavaScript functions.
So if we execute these functions we can simply do that by opening up our developer tools
under view options developer and then JavaScript console, and type in one of these commands
so for example let's go with the geoPlugin city.
It will give us back the actual city where the plug-in located us.
Now I'm in Berlin so if I go into geoPlugin country code for example, we should obviously
get de for Germany.
So a host of other functions here that you could execute some more, some less useful
to make this actually something that you could use in your tag manager account.
I'd recommend to build a custom listener function out of this.
So I've done this right here where you load the geoPlugin then execute these functions,
put them actually in variables, and then push them to the data layer.
So we'll have them available in Google Tag Manager as well.
I'm gonna link up this code down below.
So let's add this to our page, we just need to add our trigger this is pause right now
let's unpause this, save this and refresh our page.
And that code should then deploy our data to the data layer we see here our geo event.
Let's look into the data layer; we have these data layer information now available, country
name, country code, currency code, if you wanted to do anything with the currency, the
city, the region.
There's more data available in the API I didn't push all of this might be relevant to you
so look through the documentation right here.
Now what can we do with this information once it's in the data layer?
You can build a data layer variable.
So let's say we wanted to pull out this country name from the data layer how would we do that?
You can go over to Google Tag Manager in the variables menu, click on new user-defined
variable this will be a data layer variable for the country.
And we'll choose as a type the data layer variable here and enter the key which is in
our case at the country name.
Alright let's save this, refresh our page, refresh this and we should see now in our
variable menu if you go to geo here that our div country is Germany.
Now what can we do with this country and this variable?
Well obviously we could use this in our tags.
So for example if we had an event tag by Google Analytics, we could use this in our actions
for example here, just click on this brick icon and we can choose our div country and
that would be sent over to Google Analytics.
Once this tag gets fired, we could also put this into a Facebook tag for example right
here.
So if you have a custom event I could just add this on in an object it's called this
key event or country rather and then input with the two curly brackets our div country
right here.
Alright so every time a track click would be sent over to Facebook, it would also have
the custom parameter attached as country and then the country that the user came from.
So this is obviously how you can use that in tags.
We are probably most interested to use this actually in our triggers.
And in our triggers it's also not a big deal, we could also build a country trigger for
Germany and use our preferred trigger here.
Although we need to be very careful when this information is actually available.
Because if we look into our waterfall here of the different events that fire, we see
that the geo-information in the data layers only available after the page view.
Sometimes before the DOM Ready, sometimes after the DOM Ready it depends on your website
and how fast it loads and how many tags here obviously have in your Google Tag Manager
count.
Now if we wanted to use this in a trigger that fires normally on the DOM Ready or Window
Loaded that would be fine.
But any information or any event that you would use previous to the geo event would
not fire because the event data is not there yet.
So be aware of that we could go within custom events since we have already a geo event in
our data layer.
Your custom event would be geo and then only on some events where a div country equals
Germany for example.
Let's save this and then we could attach this to one of our tags for example this click
tag.
Save this and refresh our page.
And we see our geo event tag fired because our variable actually was DIV country Germany.
Now to test this out, I'm gonna open up a VPN here.
Go to the United States; refresh the page, now you see a different page here because
mine is actually running on the local host.
And we see that our listener fire but our GA even clicked didn't fire because our Geolocation
should now be the country United States.
So that works as expected and I hope you now understand how you can use this Geolocation
plug-in for your purposes to deploy tags.
Just a word of warning to some of you out there that might want to use this for compliance
purposes for GDPR or any other privacy laws out there, there is actually an add-on to
the Geolocation plug-in so you could obviously check in your conditions here is the country
one of the European countries.
But there's also an add-on to this geoPlugin which actually let's see if the user is eligible
to the cookie law.
So this would be it will come from one of the 27 European countries that have the cookie
law in place.
And that would just simply give you this cookie consent function that you could deploy in
order to see whether the user is eligible because not all the European countries are
obviously inside of the GDPR relevant zone.
And only then show them a cookie consent form and other users from other countries don't
get bothered with it.
You could definitely do this it's just more involved because you would probably like to
connect us with tags like Google Analytics or Adwords and only fired this when somebody
consents to your cookies and to being tracked.
So there were would be actually multiple operations first you would like to know is the user from
one of these countries which you can do with this geo API then show him the cookie consent
and only if he has actually confirmed it, you would send off the Google Analytics tag.
But that, in turn, would also mean that a user who is not from this European countries
would not get all this information shown but he still needs to be geolocated, which means
the earliest that you get that information is actually here and not at the page view
level.
So that would mean in turn that you would not fire your Google Analytics and your other
tracking tools at the earliest possible moment.
That would be the page view so when the page actually loads you would need to wait every
time for the Geolocation API and do your different checks whether the user wants to be tracked
or not.
And that would mean that you so have a greater discrepancy in your data quality I would say
because the later you fire such codes, the lower is the probability of the user actually
being still on the page.
He could be already moved on to the next link or left your site by the time the Geolocation
API gives back the data to you.
So just be aware of that if you want to install any kind of privacy checks with this Geolocation
API.
All right so there you have it this is how you can utilize Geolocation with the help
of Google Tag Manager.
Now I'd love to hear from you.
Why do you want to geolocate a user?
What is your specific use case?
Please leave me a comment down below because I also want to learn from you, what you are
up to, and how you utilize this technique.
And if you haven't yet, then consider subscribing right over there because we bring you new
videos just like this one every week.
Now my name is Julian.
Till next time.