0

On my website I am displaying a country specific icon for the visitor. I'm doing this by setting a data-country variable in the head of the html document and loading a javascript file that sets the local storage value once when the homepage is visited.

I only want to run the geo.js script once as it can be a performance hit, so I included the conditional, if the first segment is empty then load the geo.js file. But if the users first visit to the site is anywhere other than the homepage the geo.js file is not loaded and the country code is not selected.

Here is my current code:

<head {if segment_1 == ''}data-country="<?php $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; echo $country_code; ?>">
{if segment_1 == ''}
  <script src="{cdn_url}{exp:setversion path='/js/geo.js'}"></script>
{/if}

Here are the contents of the geo.js file:

var countrySet = localStorage.getItem('country');

if(countrySet !== null && countrySet == 'UK') {
 window.location='http://website.com/uk'; 
}

// If country is not stored, get it from the <head> element 

if(countrySet === null) {
var country = document.getElementsByTagName("head")[0].getAttribute("data-country");


// Store country

localStorage.setItem('country', country);
 if(country == 'UK') window.location='http://website.com/uk';
}

Question

Is there a way to load the geo.js script only once per site wide visit? Right now if I take away the conditional the script runs on each page load.

  • ... What? I really don't consider that a performance hit. The JS to conditional choose to load the script or not would work out to close the same amount of characters. You know what a performance hit is? Requiring a request for a JavaScript file that has less than 10 actual LOC. Just put that inline in your header. – jrothafer Oct 14 '15 at 18:47
  • You're right @jrothafer it's not a real performance hit. The phrasing is wrong. I should have said that I am trying to find a way to make the geo.js more performant so its not as noticeable. I will update this question with the proper phrasing. Do you think the geo.js script is small enough to run before every page load and be unnoticeable? – j_lemon Oct 22 '15 at 16:11
1

Use this Session add-on.

Add to your head (on every page):

{if "{exp:session_variables:get name="js_deployed"}" != "yes"}
    <script src="{cdn_url}{exp:setversion path='/js/geo.js'}"></script>
    {exp:session_variables:set name="js_deployed" value="yes"}
{/if}

As this will be on every page, your Javascript code would need to be updated to take them to the relevant (equivalant) page, not assume the homepage.

For country identification you might want to consider doing it server side with any one of these selection of add-ons.

Be very careful on how you handle this though, automatically redirecting someone is bad UX, as you're assuming they didn't want to go to their original destination (which may well have come from a search listing). You can get into 404 problems when you don't have the same page available in all languages. Even worse, if you ignore their original destination and force them to the homepage on language change. A typical scenario is I'm on holiday, I speak language X, I'm in location Y, you force location Y's language on me and I wanted to see langauge X.

Another more fundamental issue is SEO - imagine in your example you referenced "US" not "UK", every time the JS Googlebot (US based) visited your website and tried to access pages in any other languge they would be forced to the US version, thus they would never be able to index and explore the other languages.

The best solution is to not assume, present the user with a message on screen and ask them - they're the ones that will know what they want - not you!

| improve this answer | |
  • Thanks so much for this. I'll try this add-on and get back to you. And thanks so much for the advice on handling region based automatic redirects. I hadn't even considered any SEO complications. I like what you said about not assuming for the user, so I will take this advice for the future. But for things in production I will work on cleaning up the country id and making the geo.js script less noticeable. – j_lemon Oct 22 '15 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.