Geolocation app - API

StephenK
Shopify Partner
37 2 16

If you are calling it via client side code on your site, CORS should be fine.
For example if you call it using a URL like this, rather than specify a domain.

jQuery.get('/browsing_context_suggestions.json', (d) => { 
// do something more interesting than console
console.log(d);
});

 

Some Shopify/Ecommerce related articles - https://medium.com/@stephenkeable
G_Popov
New Member
2 0 1

You're a legend, thanks Stephen!!

Lassepappa
New Member
7 0 0

Hi! 

This is exactly what im looking for, a way to check country code and display different content in banners for different countries without an app.

However. Not sure how to follow your examples. 

I want to do this in theme.liquid. Would this work?

<p id="country_banner">country</p>
<script>
jQuery.get('/browsing_context_suggestions.json', (d) => { 
if (obj.d.country_name == 'SE'){

	document.getElementById("country_banner").innerHTML = 'Sweden';
}else{
	document.getElementById("country_banner").innerHTML = 'not Sweden';
}
});
</script>

 

Can you help out with this? Not sure how to call for the country name in (d) really.

 

0 Likes
StephenK
Shopify Partner
37 2 16

The callback is passing d, however you’re trying to access obj.d which hasn’t been defined.

would be

d.detected_values.country_name

Will also return Sweden rather than SE I think.

Some Shopify/Ecommerce related articles - https://medium.com/@stephenkeable
0 Likes
Lassepappa
New Member
7 0 0

Hi!

Thanks. Still not getting the script to work thou, grateful for further assistance.

I am adding this to theme.liquid just before the end body tag.

<p id="country_banner">country</p>
<script>
jQuery.get('/browsing_context_suggestions.json', (d) => { 
if (d.detected_values.country_handle == 'SE'){

	document.getElementById("country_banner").innerHTML = (d.detected_values.country_handle);
}else{
	document.getElementById("country_banner").innerHTML = 'not Sweden';
}
});
</script>
0 Likes
StephenK
Shopify Partner
37 2 16

There is no country_handle, there is country.handle however.

If you are unfamiliar with JavaScript I would recommend hiring a developer possibly.

Some Shopify/Ecommerce related articles - https://medium.com/@stephenkeable
0 Likes
StephenK
Shopify Partner
37 2 16

looks like the endpoint now returns a country object containing name and handle (ISO country code I think) in the detected_values object.

{
  "detected_values": {
    "country_name": "United Kingdom",
    "country": {
      "handle": "GB",
      "name": "United Kingdom"
    }
  },
  "suggestions": []
}

 

Some Shopify/Ecommerce related articles - https://medium.com/@stephenkeable
0 Likes
Lassepappa
New Member
7 0 0

Hi! Thanks for you help.

Still not getting this to work thou... Id rather use this function than installing yet another plugin if I can.

 

This should be all i need i think but no...

 

<p id="country_banner">country</p>
<script>
jQuery.get('/browsing_context_suggestions.json', (d) => { 
if (d.detected_values.country.handle == 'SE'){

	document.getElementById.("country_banner").innerHTML = (d.detected_values.country.handle);
}else{
	document.getElementById.("country_banner").innerHTML = 'not Sweden';
}
});
</script>
0 Likes
StephenK
Shopify Partner
37 2 16

As would be shown in the console, there is a syntax error with a dot before the opening bracket here:-

document.getElementById.("country_banner").innerHTML

It should just be

document.getElementById("country_banner").innerHTML

 As mentioned before if you are not familiar with JavaScript you should probably hire a developer who is.

Some Shopify/Ecommerce related articles - https://medium.com/@stephenkeable
0 Likes
Lassepappa
New Member
7 0 0

Yeah sorry!

Fixed that however i get jquery no loaded. I thought this was natively supported in shopify?

Do i need to include a Jquery lib in the head section or?

Kindly / Lasse

0 Likes