Last active
September 28, 2022 20:06
-
-
Save boldsupport/64a00464b46af660967d6ee3ded2ea1f to your computer and use it in GitHub Desktop.
Cashier Startup Script for Klaviyo Integration
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
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@boldsupport this code doesn't work anymore