Geo Targeting With Javascript

Easy Geo Targeting With Javascript

Geotargeting on the web refers to delivering custom content based on the visitors geographical location.

That “custom content” generally comes down to flights, places nearby or pinpointing you location in apps (Uber, Lyft etc) - it will definitely be used in the headline.

If you ever wondered how a website knows where are you from, that's geo targeting.

Pin It

searching for carpenter on google; geo targeted results

If you are in New Orleans and would search on Google for a “carpenter”, after the ads the first results are local searches.

searching for a flight from New Orleans on google; geo targeted search results

Same thing applies to flights.

Although these are very common uses for geo targeting (and search intent in this particular case), it can also be used to filter down the visitors you for an email list or give an extra reason for someone to sign up by using a good old Cialdini principle: unity.

How To Do Geo Targeting with Javascript

The simple code that makes Geo Targeting possible is at

The first thing to do is to initialize the javascript by inserting the following code into the page between the <head> and </head> tags: <script language="JavaScript" src="" type="text/javascript"></script>

After that by calling simple variables one can create custom content.

Geo Targeting and Email Marketing

Most email marketing services charge a monthly fee based on the size of the list. The more people are on the list, the more expensive the subscription.

Fact: visitors from US and Canada are the best potential leads.
Any ad serving platform will pay more on CPM or clicks from these two countries.

Generally your email marketing provider will supply a javascript code to display a nice modal pop-up for your visitors to sign up (or download a pdf etc.)

Sooo, if you would like to have on your list only visitors from US and CA, you would implement the following code:

if (geoplugin_countryCode() == "US" || geoplugin_countryCode() == "CA"){
document.write('<script type="text/javascript" src="/LinkProvidedByAutoresponderServices"></scr'.'ipt>');
else {
//do nothing

In this particular case if a visitor comes from Germany (DE) nothing will happen.

The same can be applied to target a specific state, area code, currency code or continent.

If you implement this on a EU website please ensure you comply with GDPR regulations - the same javascript principle can be applied to display cookie consent for European visitors.


If you ever thought of geo targeting this is a good time to take action and implement the above free solution.

Let me know in the comments if you need any help with the code


Pin It