Rich-text Reply

document onbeforeunload event to trigger optimizely custom event

cstevenson 11-06-15

document onbeforeunload event to trigger optimizely custom event

All,

I am looking to add a custom event goal that is tied to the document onbeforeunload event. Has anyone ever set this up successfully to fire the custom event before the user leaves the page? 

 

window.onbeforeunload = function(e) {
window.optimizely.push(["trackEvent", "myCustomEvent"]);
};

 

If it is possible wondering the best way to preview and validate this is since the event tracking console is gone when navigation to a new page from the existing experiment page.

 

 

tomfuertes 11-06-15
 

Re: document onbeforeunload event to trigger optimizely custom event

No reason it shouldn't work! We usually testing things like this by pushing an experiment live to a `domain.com/fake-test-page-that-404s-and-has-the-snippet`, visiting it, and then seeing if 1x visitor, and 1x conversion to that goal was logged.

____
- Tom Fuertes | CTO @ CROmetrics / LinkedIn
"Most Impactful Use of Personalization" and "Experience of the Year" Optie award winner.


robertchan 11-09-15
 

Re: document onbeforeunload event to trigger optimizely custom event

Agreed with TOMFUERTES, this should work without issues. Something you might also want to use to check whether methods exist in the future is as follows:

optimizely.$.fn["TYPE THE METHOD HERE"]

e.g.

optimizely.$.fn["asldkfja"]
returns undefined

This would help to make sure the methods you're using work especially for elements that need to be conditionally activated.
Robert Chan

Aicke 12-02-15
 

Re: document onbeforeunload event to trigger optimizely custom event

Regarding "since the event tracking console is gone when navigation to a new page from the existing experiment page."

 

Both Firefox and Chrome hav an option to preserve the console log while navigation to another page. On Chrome it's just above the Dev Tools Console Output, in Forefox it's behind the gear icon in the Dev Tools.

Everything is hard, before it is easy.
Level 2