Optimizely Revenue Tracking jQuery snippet

936 views Asked by At

I am trying to enable revenue tracking in Optimizely.

I have customized the jQuery snippet that the Optimizely knowledge base provides and installed it on the last page prior to the completion of the checkout process.

Here is the "Complete Booking" button that I am trying to connect my snippet to:

onmousedown I want to fire this function:

<script type="text/javascript">
  var optimizely = optimizely || [];
  $("input[value='Complete Booking'].submit").live("mousedown", function() {
     optimizely.push(['trackEvent', 'booking_complete', total]);
  });
</script>

There is a variable called total that exists elsewhere in the DOM that has the total price of the item purchased.

However, when I install this script, it does not track revenue. I suspect a jQuery issue in my rewrite of the official Optimizely function. Does anyone have some insight into this?

Update

I realized that I probabally need to call $(document).ready(function() in order for the jQuery to trigger. I also am now passing the value in cents rather than dollars. Also tried using .submit() rather than .onmousedown(). I also changed the value in the push to add_cart_button_clicked, just like in the revenue tracking tutorial on Optimizely's site. Here is my updated code, but it is still not working.

<script type="text/javascript">
    $(document).ready(function(){
        var optimizely = optimizely || [];
        var revenueInCents = total * 100;
        $("input[value='Complete Booking'].submit").submit(function() {
            optimizely.push(['trackEvent', 'add_cart_button_clicked', revenueInCents]);
        });
    }
</script>
1

There are 1 answers

0
TomFuertes On

Optimizely's changed their revenue tracking API since this question was posted.

https://help.optimizely.com/hc/en-us/articles/200039865-Revenue-Tracking

$(function(){
  window.optimizely = window.optimizely || [];
  var revenueInCents = total * 100;
  $("form#id").submit(function() {
    window.optimizely.push([
      'trackEvent', 
      'add_cart_button_clicked', 
      {revenue: revenueInCents}
    ]);
  });
});