Created
March 10, 2015 05:05
-
-
Save bialecki/57fafcc251564365629f to your computer and use it in GitHub Desktop.
Track Started Checkout for Volusion in Klaviyo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Add this script to 'one-page-checkout.asp' --> | |
<script type="text/javascript"> | |
var _learnq = _learnq || []; | |
// Get this from https://www.klaviyo.com/account#api-keys | |
_learnq.push(['account', 'PUBLIC_API_KEY']); | |
(function () { | |
var b = document.createElement('script'); b.type = 'text/javascript'; b.async = true; | |
b.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'a.klaviyo.com/media/js/analytics/analytics.js'; | |
var a = document.getElementsByTagName('script')[0]; a.parentNode.insertBefore(b, a); | |
})(); | |
</script> | |
<script type="text/javascript"> | |
var _learnq = _learnq || []; | |
var trackStartedCheckout = function () { | |
$.post('/AjaxCart.asp', function (data) { | |
if (!data || !data.Products || !data.Products.length) { | |
return; | |
} | |
var items = []; | |
$.each(data.Products, function (i, record) { | |
var item_price = +(record.ProductPrice.replace(/[\$,]+/g, '')) / record.Quantity; | |
items.append({ | |
SKU: record.ProductCode, | |
Name: record.ProductName, | |
Quantity: +record.Quantity, | |
ItemPrice: item_price, | |
RowTotal: item_price * record.Quantity, | |
ImageURL: location.origin + '/v/vspfiles/photos/' + record.ProductCode + '-2T.jpg' | |
}); | |
}); | |
_learnq.push(['track', 'Started Checkout', { | |
$value: +(data.Totals[0].CartTotal.replace(/[\$,]+/g,"")), | |
Items: items | |
}]); | |
}, 'json'); | |
}; | |
$('[name="OnePageCheckoutForm"] [name="Email"]').change(function (e) { | |
var email = $(this).val(); | |
// Do some light validation. Klaviyo will do more validation when the data is received. | |
if (email && /@/.test(email)) { | |
_learnq.push(['identify', { $email: email }]); | |
trackStartedCheckout(); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment