Rich-text Reply

Optimizely Data Object Visitor Location is Null

dougmacklin 07-26-16
Accepted Solution

Optimizely Data Object Visitor Location is Null

Hi there,

 

I'm trying to access the visitor's location property, part of the Optimizely data object:

 

window['optimizely'].data.visitor.location

 

However it is returning null for me, even though I am on an Enterprise plan. Here is the entire optimizely.data.visitor object that I can see:

 

{
  "browser": "Chrome",
  "browserVersion": "51.0.2704.103",
  "device": "unknown",
  "deviceType": "desktop",
  "platform": {
    "id": "mac os",
    "version": "10.11.4"
  },
  "mobile": false,
  "mobileId": "unknown",
  "os": "mac os",
  "lists": {},
  "location": null,
  "ip": null,
  "matchingRules": null,
  "referrer": "",
  "segments": {
    "1571113867": "search",
    "1573884183": "false",
    "1580510026": "gc",
    "1856490740": "gc",
    "1871620897": "none",
    "1872590752": "false",
    "1873520688": "referral",
    "1992220011": "none",
    "2011250403": "true",
    "2338360045": "true",
    "2340150412": "true",
    "2701080337": "false",
    "2702650321": "gc",
    "2704480408": "none",
    "2705810351": "direct",
    "2706020152": "false",
    "2706560317": "none",
    "2707460365": "gc",
    "2708140383": "false",
    "2708290386": "referral",
    "2717810225": "none",
    "2718550206": "false",
    "2719510233": "gc",
    "2721320256": "referral",
    "2723730053": "direct",
    "2727730008": "gc",
    "2729180004": "none",
    "3713124431": "gc",
    "3728264737": "search",
    "3734713784": "false",
    "3735704323": "none",
    "3735872471": "none",
    "3744262300": "false",
    "3745172258": "campaign",
    "3748250031": "gc",
    "3771520742": "none",
    "3771710746": "false",
    "3777040790": "direct",
    "3788520753": "gc",
    "3823971587": "gc",
    "3830381821": "none",
    "3834972686": "false",
    "3835011530": "search",
    "4998950102": "true"
  },
  "dimensions": {},
  "audiences": {},
  "params": {
    "": ""
  }
}

 

This solution posted by @Becca_Bruggman last year is pretty much exactly what I'm trying to accomplish, however I can't with a null location. Please advise, any help would be greatly appreciated.

 

Thanks in advance,

Doug

MartijnSch 07-27-16
 

Re: Optimizely Data Object Visitor Location is Null

Hi Doug,

Have you tested this on multiple machines/ with multiple users? What can happen sometimes is that based on the IP address and the other information of the user that it still won't be able to locate the phsyical location of the user. That's probably why the return is NULL in this case. These libraries are usually not 100%.
dougmacklin 07-27-16
 

Re: Optimizely Data Object Visitor Location is Null

[ Edited ]

Thanks for your reply @MartijnSch, however I can assure you that is not the issue. I have tested it from multiple devices / internet connections and run into the same issue. More importantly, when I visit another site that uses Optimizely, for example The New York Times, I can access the same optimizely.data.visitor.location variable from the console and it returns a non-null, relatively correct location value. When I try doing the same on my site, warriors.com, it returns null. Is there some piece of setup that I'm missing?

 

Thanks in advance,

Doug

David_Orr 07-27-16
 

Re: Optimizely Data Object Visitor Location is Null

Hi Doug,

The data object will only return with a value if you have an experiment with an IP Address audience condition or Geo Targeted location. The reason is because our Geo snippet will not fetch for the location data unless there's at least 1 active experiment with a geo targeted / IP audience.

Please be aware, the geo location may not be available immediately when the snippet loads. The reason is because it needs to fetch the data from a 3rd party.

David
Senior Technical Support Engineer
Optimizely
dougmacklin 07-27-16
 

Re: Optimizely Data Object Visitor Location is Null

Okay makes sense @David_Orr, thanks for the reply. I was hoping there would be a way to leverage the Optimizely data object to get site visitors' locations without burning through my monthly experiment unique visitors limit, but I guess that's not the case. Let me know if there are any alternatives that I'm missing.

 

Thanks again,

Doug

David_Orr 07-28-16
 

Re: Optimizely Data Object Visitor Location is Null

Doug,

You do not have to burn through your monthly uniques. The experiment with the geo-targeted condition doesn't even have to match an actual part of the site. Optimizely just needs to see at a high level that the project has an active experiment with a geo targeted audience condition.

I recommend that you try it yourself by creating an experiment using your site's URL, add a geo location audience condition and then open the URL Targeting settings and type the word null and click apply.

In order for a visitor to attribute to the monthly MUV, they must qualify for an experiment. That means they must match the Activation, URL Targeting, Audience, and Traffic allocation conditions.

David
Senior Technical Support Engineer
Optimizely
dougmacklin 07-28-16
 

Re: Optimizely Data Object Visitor Location is Null

Awesome! Thanks @David_Orr that seems to be working perfectly. One quick followup, you mentioned that the location might not be ready immediately when the snippet loads, is there a callback function or something I could use to ensure that it's ready when I need it?

David_Orr 07-28-16
 

Re: Optimizely Data Object Visitor Location is Null

You're welcome @dougmacklin. Unfortunately, there is no callback available when the location data is available. I recommend creating an interval that checks to see if the object doesn't equal to null and have it time out after a couple seconds.

 

 

Senior Technical Support Engineer
Optimizely
dougmacklin 07-28-16
 

Re: Optimizely Data Object Visitor Location is Null

Makes sense, will do. Thanks again @David_Orr!

dougmacklin 07-29-16
 

Re: Optimizely Data Object Visitor Location is Null

[ Edited ]

@David_Orr, after setting up an experiment with a location-conditioned Audience as you described above, I ended up using a Promise combined with setInterval() to ensure that the location was ready before I tried to access it:

 

 

// assign to the global scope like the optimizely data object
window.optimizelyLocation = new Promise((resolve, reject) => {
  // check to see if it is already ready
  if (typeof userLocation !== "undefined") resolve(userLocation);

  // otherwise check every 100 milliseconds, for a maximum of 2 seconds
  else {
    var counter = 0;
    
    var timer = setInterval(() => {
      var userLocation = optimizely.data.visitor.location;

      if (typeof userLocation !== "undefined") {
        clearInterval(timer);
        resolve(userLocation);
      }

      else if (counter >= 20) {
        clearInterval(timer);
        reject('location null');
      }

      counter++;
    }, 100);
  }
});

 

This can then be used like so:

 

window.optimizelyLocation.then((res) => {
  // your user-location based code, eg:
  console.log(res); // will log something like {continent: "NA", country: "US", region: "CA", city: "SANFRANCISCO"}
});

I uploaded this to github and npm in case anyone else is facing the same issue. Let me know if you have any suggestions. Thanks again!