Business intelligence & user acquisition insight delivered to your inbox:


Optimizely Custom Event: Successful Email Submission

When I’m working on a user acquisition project, I use Optmizely to A/B test the email capture landing page.  I always create a custom event that triggers when the user successfully submits an email. Unfortunately, Optmizely doesn’t have a proper API which makes doing this difficult without redirecting to a new page.

It took me awhile to figure out a workaround in Javascript so I wanted to save everyone else the trouble:

First create a regex function to validate an email

function validateEmail(email) {
     var valid_email = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
     return valid_email.test(email);
}

Next, create a function that fires when the user clicks the submit button

 $( document ).ready(function() {
     $("input[type=submit]").bind("mousedown", function validate () {
     }):
}):

Put the entered email into a variable

var email = $("input[type=text]").val();

Write an if statement that uses our ValidEmail function to test if the email is valid

if (validateEmail(email)) {

If the email is valid, send the event to Optimizely

if (validateEmail(email)) {
     window['optimizely'] = window['optimizely'] || [];
     window.optimizely.push(['trackEvent', 'email_submit'])

In the last line we are calling our event ‘email_submit’. You can change this to any event name that you want.

All together it looks like this:

function validateEmail(email) {
     var valid_email = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
     return valid_email.test(email);
}

$( document ).ready(function() {
     $("input[type=submit]").bind("mousedown", function validate () {
     var email = $("input[type=text]").val();
          if (validateEmail(email)) {
               window['optimizely'] = window['optimizely'] || [];
               window.optimizely.push(['trackEvent', 'email_submit'])
          }
     });
});

In Optimizely create a new custom event goal. Set “Custom Event to track” to email_submit.

optimizely custom event


No responses yet

Leave a Reply


Murad Reviews
Exposed Skin Care Reviews
Proactive Reviews
How To Get Rid Of Acne
How To Get Rid Of Stretch Marks
How To Get Rid Of Blackheads
how do i get rid of stretch marks
How To Make Stretch Marks Go Away
How To Get Rid Of Stretch Marks On Breasts
Stretch Marks After Pregnancy
How To Prevent Stretch Marks During Pregnancy
How to Get Rid of Stretch Marks