I want to redirect users, using IPGeolocation API to go to different country landing pages on my site.
One thing:
The ‘import’ line below is ‘greyed out’ on my site. See screenshot.
Is that a problem?
Here is the original code below. It doesn’t redirect at the moment when live. Nothing happens.
NOTE: I had to put spaces in the URL api.ipgeolocation below in my code because this forum didn’t allow me to post links. So yes, that’s normally not broken.
import wixHttpFunctions from ‘wix-http-functions’
export function getVisitorIP ( request ) {
const ip = request . headers [ ‘x-real-ip’ ] || request . headers [ ‘x-forwarded-for’ ] || request . ip ;
return ip ;
}
export function locationRedirect ( request ) {
const ip = getVisitorIP ( request );
const locationData = await fetch ( https:// api.ipgeolocation. io/ipgeo?apiKey=16ed0b8444e64bc7b879ce2c3c255c7b&ip= ${ ip });
const location = await locationData . json ();
const countryCode = location . country_code2 ;
if ( countryCode === "US" ) {
**return** {
"status" : 301 ,
"headers" : {
"Location" : "/us-page"
}
}
} **else** {
**return** {
"status" : 301 ,
"headers" : {
"Location" : "/international-page"
}
}
}
}
