Rich-text Reply

How to trigger a custom event defined outside optimizely

CouchPsycho 01-11-16
Accepted Solution

How to trigger a custom event defined outside optimizely

Hi there,

 

to be honest -before i start exercising unappropriate violence against my keyboard- ;-) i post this probably simple question. I am trying to add a click-handler to an element which triggers a custom event defined outside optimizely as part of the overall website implementation.

 

This code is not working:

 

$(".header > .headingType1").click(

function() 
{ 
$(document).trigger(TRAVEL_DATA_INPUT_VISIBILITY_CHANGED, false);
}

);

 

The click-function is added to the element and executed. But the custom event is not triggered. If i execute the trigger-line in chromes console, it works....

 

Any ideas?

 

Thank you in advance....

 




"the essence of the creative act is to see the familiar as strange." (anonymous)
robertchan 01-11-16
 

Re: How to trigger a custom event defined outside optimizely

What exactly is the custom event? Have you added the custom event to your goals, and if so, how?
Robert Chan

CouchPsycho 01-11-16
 

Re: How to trigger a custom event defined outside optimizely

I do not refer to an optimizely event but to one regarding the whole document/page which is modified:

TRAVEL_DATA_INPUT_VISIBILITY_CHANGED



"the essence of the creative act is to see the familiar as strange." (anonymous)
robertchan 01-11-16
 

Re: How to trigger a custom event defined outside optimizely

Run your experiment with a test cookie then check in the network tab of your console and filter by XHR then filter by the word event. Check the value of n for each of the events to see whether your custom event shows up. If not, then you can confirm that the event's not recording. If your event is part of an iFrame, you may have some issues with tracking that. One other reason it may not be tracking is that you're using $(document) - have you tried scoping out to window?
Robert Chan

JDahlinANF 01-11-16
 

Re: How to trigger a custom event defined outside optimizely

[ Edited ]

Any time something works in the console but does not work when added as part of the variation code, it means one thing (I hate to speak in absolutes, but this has universally been the issue every time it has happened to me):

 

"The element you are binding to does not exist when the Optimizely code runs".

 

 

1- Verify your initial binding works

$(".header > .headingType1").click(function(){alert('header headingType1 clicked');)

2- You have "TRAVEL_DATA_INPUT_VISIBILITY_CHANGED" as the trigger but do not have quotes around it.  Perhaps this is a type in your OP.  But, if you are trying to invoke a trigger with a name that is the value of that vairable, then it is could be that the variable does not exist when the Optimizely Code runs, therefore it is throws an error.

 

3- The easiest way to resolve this is to simply change all of your jQuery scopes to "window", which will delay code the code's execution until after the DOM is fully loaded.  Since you are not changing the appearance of the page, delaying the binding ought not introduce any flashing to the user.

 

window.$(".header > .headingType1").click(function() { 
    window.$(document).trigger('TRAVEL_DATA_INPUT_VISIBILITY_CHANGED', false);
});

 

CouchPsycho 01-12-16
 

Re: How to trigger a custom event defined outside optimizely

Great. This works as intended....

 

Thank you very much.




"the essence of the creative act is to see the familiar as strange." (anonymous)