Skip to content

Instantly share code, notes, and snippets.

@boldsupport
Last active September 28, 2022 20:06
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 boldsupport/64a00464b46af660967d6ee3ded2ea1f to your computer and use it in GitHub Desktop.
Save boldsupport/64a00464b46af660967d6ee3ded2ea1f to your computer and use it in GitHub Desktop.
Cashier Startup Script for Klaviyo Integration
<script async type="text/javascript" src="//static.klaviyo.com/onsite/js/klaviyo.js?company_id=*YOUR COMPANY ID*"></script>
<script>
var intervalCount = 0;
const STARTED_CHECKOUT_EVENT = 'Started Checkout';
var intervalId = setInterval(() => {
if (typeof window._learnq !== 'undefined') {
clearInterval(intervalId);
var _learnq = window._learnq || [];
const items = [];
const itemNames = [];
_learnq.identify({
'$email': BOLD.order.customer.email
}, undefined, undefined, () => {
window.BOLD.order.line_items.forEach(function (item) {
itemNames.push(item.product_title);
items.push({
"SKU": item.sku,
"ProductName": item.product_title,
"Quantity": item.quantity,
"ItemPrice": (item.price / Math.pow(10, 2)).toFixed(2),
"RowTotal": (item.total_price / Math.pow(10, 2)).toFixed(2),
});
});
let klaviyoPayload = {
"$event_id": BOLD.order.public_order_id,
"$value": (BOLD.order.total / Math.pow(10, 2)).toFixed(2),
"ItemNames": itemNames,
"ResumeUrl": BOLD.order.resumable_url,
"Items": items
};
_learnq.track(STARTED_CHECKOUT_EVENT, klaviyoPayload);
console.log(`KLAVIYO: sent ${STARTED_CHECKOUT_EVENT} command`);
console.log(klaviyoPayload);
});
} else {
if (++intervalCount > 20) {
console.log('Unable to load Klavio script.');
clearInterval(intervalId);
}
}
}, 500);
</script>
@titibouboul
Copy link

@boldsupport this code doesn't work anymore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment