REMINDER
Retrieve visitor country code?
-
Is it possible to get the visitors country from the back-end?
I know it's available from CloudFront-Viewer-Country and can be passed to the Lambda.
-
You could try looking in the headers. If not, the best thing to do would probably do a Geo IP lookup, you can indeed get the IP from the backend. I don't think browser headers send country code, they barely send timezone info
I think I have code for extracting the IP address if you need it, but if you print all the headers from the request you should see how to access it.
Cheers @ThomasD
-
Yeah, I can use a GeoIP lookup service since the IP is in the headers.
Just thought I'd avoid the extra cost since it's already available in AWS.
-
In case you haven't found this out yourself yet, you can access it from within a CloudBackend function using:
event["HEADERS"]["CloudFront-Viewer-Country"]
Hope this helps!
-
Glad you wrote that!
I looked for it using the "Try" feature, but that doesn't return the CloudFront headers. I would have missed it for sure.
Problem is, this will eat up the back-end quota instantly because every visitor would need to execute it...
-
Can you help me understand the use-case a bit more? Maybe I'll get a flash of inspiration if I know more about what you're trying to do
-
Wanted to detect the country so I can apply the correct language automatically.
@Wassim Maybe that is already done?
Google seem to be very picky about localization:
https://developers.google.com/search/docs/advanced/crawling/managing-multi-regional-sites
-
@Thomas-Djรคrf said in Retrieve visitor country code?:
Wanted to detect the country so I can apply the correct language automatically.
Yes language is automatically detected and a cookie "lng" is set. Then it depends on your page translations from the page builder.
-
Super, then I don't need to worry about that