1

I have a problem.

I have a piece of code, that I send to websites to be placed inside the <head></head> right at the very beginning of the header. My code will run and collect some data and pass the result to the rest of the page (ad servers, forms, etc., for example).

My problem is that I need to limit the number of times this code can make calls back to my server and that needs to be based on user location (city, more specifically). I cannot use the ad server, and benefit from its targeting capabilities because I do need to run this code before everything else.

I was thinking if there was any sort of script that would allow me to identify the user location and make the decision to whether or not to call my server, therefore making sure that I am only using my resources when it is actually needed.

I thought about using the html5 geolocation API but it requires user interaction. Then I was thinking about using a service like IpRegistry.co, to:

  • query for the location and if ;
  • location=desired location;
  • then load javascript code;
  • otherwise do nothing;

I am not sure if this is possible and how to do it. Any help would be greatly appreciated and desired. I am ready to explore opportunities here.

If I didn't stress enough, it needs to happen inside the header, not on the body.

Thanks in advance.

PC

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Paulo Capelo
  • 1,062
  • 1
  • 9
  • 13
  • You cannot use the website server to add or not some code based on your requirements? – Manuel Romeiro Oct 08 '21 at 20:29
  • The idea is to not deplete my resources, I cannot build infrastructure as big as a few of the websites I am going to work with just to interact with a small portion of it. I am trying not to have to listen to all requests and only the ones that interest me. Some sites are capable of doing that by themselves but some aren't. A client-side approach is not ideal but it could be a good solution. – Paulo Capelo Oct 09 '21 at 01:45
  • You can make a synchronous ajax request to check the location, but that request should be to the same server of the page, or you can have troubles about cross domain ( https://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain ). After this, you can use cookies to cache this validation, but maybe you cannot use them previous than the user accept it (read about cookies law) – Manuel Romeiro Oct 11 '21 at 08:55
  • Pls edit ur question to make it reproducible. Problem is questions must be specific and focused so they can be searched and be used by others. u just asked all ur problems with the code in one question. – M.Mavini Oct 16 '21 at 09:35

0 Answers0