Add conversion script
The are two common ways to implement our conversion script:
- Send purchase information directly after the site has loaded. For example on a "Thank you for your purchase" page.
- Send purchase information after some event has been triggered, e. g. the payment confirmation of some payment service.
We describe both integration methods below.
Send bought products on page load
The product objects below adhere to the specified payload format mentioned on the Conversion script functions page.
This code will be inside your normal JavaScript environment.
<script>
const sendBoughtProducts = async (products) => {
await smartfit.conversion.initialize({ apiKey: 'YOUR_API_KEY_HERE' });
const ski = products.filter(product => product.productType === 'ski');
for (const singleski of ski) {
await smartfit.conversion.sendPurchaseInformation(singleski);
}
};
</script>
This will include our conversion script to your website.
<script
src="https://widgets.onlinesizing.bike/conversion.js"
onload="sendBoughtProducts(products)"
></script>
Send bought products after some event
This code will be inside your normal JavaScript environment.
<script>
// This is a placeholder for some payment service or similar
// which triggers the event to send the bought products
paymentService.addEventListener('paymentConfirmed', async () => {
await smartfit.conversion.initialize({ apiKey: 'YOUR_API_KEY_HERE' });
const ski = products.filter(product => product.productType === 'ski');
for (const singleski of ski) {
await smartfit.conversion.sendPurchaseInformation(singleski);
}
});
</script>
This will include our conversion script to your website.
<script src="https://widgets.onlinesizing.bike/conversion.js"></script>
Integration in Google Tag Manager
Due to multiple limitations in the language features of Google Tag Manager (or GTM in short) multiple adjustments to the above examples are required. Generally you need to create a "Custom HTML" tag and copy the following into it:
<script>
function sendBoughtProducts() {
var SmartfitApiKey = '<API_KEY_HERE>'; // <= Enter your OZ API key here
smartfit.conversion.initialize({ apiKey: SmartfitApiKey }).then(function() {
var ecommerce = {{SmartFit Ecommerce}}; // This is a variable from your GTM environment
// We assume the following structure:
/* var ecommerce = {
currency: 'EUR',
items: [
{
price: 849.00,
quantity: 1,
size: '178cm',
name: 'Scott Pure Tour 100 Ski',
code: '123456789',
productType: 'ski'
},
{...}
]
}; */
var ski = ecommerce.items
.filter(function(item) { return item.productType === 'ski' }) // We only want to send ski
.map(function(item) {
return {
currency: ecommerce.currency,
productCode: item.code,
price: item.price,
quantity: item.quantity,
purchasedSize: item.size,
productType: 'SKI',
productTitle: item.name
}
});
// No we send the information for each ski
Promise.all(ski.map(function(singleski) {
return smartfit.conversion.sendPurchaseInformation(singleski);
}));
});
}
// We need the following code to load our conversion script on your site.
// (A simple script tag with an onload event does not work in GTM unfortunately)
var script = document.createElement('script');
script.type = 'text/javascript';
script.addEventListener('load', function(event) {
sendBoughtProducts();
});
script.src = 'https://widgets.onlinesizing.bike/conversion.js';
document.getElementsByTagName('head')[0].appendChild(script);
</script>