Jump to content

I need help with code on my website to redirect geo located visitors to an alternate page.

Go to solution Solved by jpeter,

Recommended Posts

Hi

I've added some code to several pages of my website to send visitors only from Australia to an alternate page. (my affiliate links are different for Aussies).

I've added the code in the page settings and saved it. It works once but then it doesn't work after that. Also when I try and edit the redirected pages the page partly loads but then goes blank on me unless I use a VPN to show that I'm not in AU.

https://www.louisedemasi.com/my-art-supplies

Can someone help? Here is the code I've used. See attachment

Screenshot 2024-01-03 at 4.28.14 pm.png

Edited by DomDom282
added website
Link to comment
  • DomDom282 changed the title to I need help adding code to my website to redirect visitors from Australia to an alternate page.
  • 3 weeks later...
  • Replies 3
  • Views 1.9k
  • Created
  • Last Reply

Top Posters In This Topic

  • DomDom282 changed the title to I need help with code on my website to redirect geo located visitors to an alternate page.
  • Solution

@DomDom282 It looks like your website uses Ajax to load new content in without refreshing the page. Because it's using Ajax to refresh the page, you'll want to wrap your code with window.Squarespace.onInitialize(Y, function() {}) . This will cause the code to re-run on every ajax request.  Added new JS code below that should resolve your issue. 

New JavaScript

window.Squarespace.onInitialize(Y, function() {
  fetch('https://ipapi.co/json/')
    .then(response => response.json())
    .then(data => {
      if (data.country_code === 'AU') {
        window.location.href = 'https://www.louisedemasi.com/my-art-supplies-au';
      }
    })
    .catch(err => console.log('Error:', err));
});

Be sure to keep the JS code wrapped in <script> tags like so:

<script>
  // Add JS code here
</script>

 

Full stack developer who loves helping people out with anything web related. If you'd like to support me, buy me a coffee!

Link to comment
  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.