Rich-text Reply

Ghostery causing JS errors

lelkins001 07-11-16

Ghostery causing JS errors

When a user is blocking Optimizely using something like Ghostery, my JS code fails and impacts other important page functionality.  How can I rewrite this to prevent references to .data from failing?

 

 

if (window['optimizely']) {
  // ensures the optimizely object is defined globally using window.optimizely.
  window['optimizely'] = window['optimizely'] || [];

 

  //Optimizely - get experiment & variation names
  var experimentName = [],
  variationName = [],
  variationNamesMap = window['optimizely'].data.state.variationNamesMap,
  i = 1;

 

  $.each(variationNamesMap, function(key, element) {
  experimentName[i] = window['optimizely'].data.experiments[key].name;
  variationName[i] = element;
  i++;
});
}

Brian_Abad 07-12-16
 

Re: Ghostery causing JS errors

Thanks for your question! I'm not overly familiar with Ghostery but to my understanding it blocks 3rd party scripts from running on a page - is this correct? If so, then the Optimizely snippet will not run within the user's browser and the Optimizely Data Object will also not be defined. This leads to references to .data not working as well.

This is outside the scope of what can be done within Optimizely as the 3rd party extension is blocking not only Optimizely but whatever scripts the user specifies.

Let me know if you have further questions!
---
Brian Abad
Manager, Technical Support
Customer Success
Optimizely
Sergio_m 03-29-17
 

Re: Ghostery causing JS errors

Hi Brian

 

Of course you can fix it. Ghostery blocks the connection from the browser to Optimizely, so the object window.optimizely exists, but not the data.

 

In addition to 

typeof window.optimizely.data.projectId !== 'undefined'

just add

typeof window.optimizely.data !== 'undefined'

and it will work.

Level 1