Skip to content

Instantly share code, notes, and snippets.

@Danny-Driscoll
Created December 21, 2016 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Danny-Driscoll/ec3c88da8c136f21527ebd706973b745 to your computer and use it in GitHub Desktop.
Save Danny-Driscoll/ec3c88da8c136f21527ebd706973b745 to your computer and use it in GitHub Desktop.
//Define Function to get jQuery and bind events after Optimizely is initialized
var bindEvents = function(){
var $ = optimizely.get('jquery');
$(document).ready(function() {
console.log('Dom Ready');
if (window.location.href === 'http://www.relatedrentals.com/' || window.location.href === 'http://www.relatedrentals.com/apartment-rentals/apartment-search') {
//Bind to the submission on the two forms
console.log('URL Match');
$('#SearchAptBasicForm,#SearchAptForm').bind('submit', function() {
console.log('Form Submit');
//Get the search values from the form fields
var city = $('.box:contains("SEARCH") .styledComboBox label').text();
var neighborhoods = $('.box:contains("Neighborhood") label,.head > .heading > .checked').text();
var bedrooms = parseInt($('.box:contains("BEDROOMS") label').html());
var min_price = parseInt($('#Price_slider label:eq(0) span').html().replace(',', ''));
var max_price = parseInt($('#Price_slider label:eq(1) span').html().replace(',', ''));
window['optimizely'] = window['optimizely'] || [];
window['optimizely'].push({
type: "event",
eventName: "apartment_search",
tags: {
city: city,
neighborhoods: neighborhoods,
bedrooms: bedrooms,
min_price: min_price,
max_price: max_price
}
});
console.log('Event Sent');
});
}
});
//Bind to AjaxComplete and check the URL for Contact Submit Custom Event
window.$(document).ajaxComplete(function(event,xhr,options){ if(options.url.indexOf("/contact-us/SendContactUsShared") > -1){window['optimizely'] = window['optimizely'] || [];
window['optimizely'].push({
type: "event",
eventName: "contact_submit"
});}});
};
//Set a listener for Optimizely to initialize and call bindEvents in the handler
window["optimizely"] = window["optimizely"] || [];
window["optimizely"].push({
type: "addListener",
filter: {
type: "lifecycle",
name: "initialized"
},
// Add the initialized function as a handler.
handler: bindEvents
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment