Created
May 5, 2024 08:44
-
-
Save rafalzawadzki/fb1ca348690952462076322d52fde007 to your computer and use it in GitHub Desktop.
π Lemon Squeezy iFrame Listener - Google Ads conversion tracking, Google Analytics purchase event tracking
This file contains hidden or 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> | |
// Listen for message from the iframe | |
window.addEventListener('message', function(event) { | |
// Check if the message is the checkout success event | |
if (event.data.event && event.data.event === 'Checkout.Success') { | |
var orderDetails = event.data.data.order.data.attributes; | |
var orderItems = event.data.data.order.data.attributes.first_order_item; | |
var userNameParts = orderDetails.user_name.split(' '); | |
var lemonFirstName = userNameParts.shift(); // Get the first name | |
var lemonLastName = userNameParts.join(' '); // Combines the remaining parts as the last name | |
// Clear the previous ecommerce object in the data layer | |
dataLayer.push({ ecommerce: null }); | |
// Format and push the new ecommerce object to the data layer | |
dataLayer.push({ | |
event: "purchase", | |
lemonEmail: orderDetails.user_email, | |
lemonFirstName: lemonFirstName, | |
lemonLastName: lemonLastName, | |
ecommerce: { | |
transaction_id: orderDetails.identifier, | |
value: orderDetails.total / 100, // Assuming the total is in cents | |
tax: orderDetails.tax / 100, // Assuming the tax is in cents | |
shipping: 0, // Add shipping cost here if applicable | |
currency: orderDetails.currency, | |
coupon: "", // Add any coupon code if applicable | |
items: [{ | |
item_id: orderItems.product_id.toString(), | |
item_name: orderItems.product_name, | |
affiliation: 'Lemon Squeezy', // Replace with your store name | |
coupon: "", // Add any item-specific coupon code if applicable | |
discount: orderDetails.discount_total / 100, // Assuming the discount is in cents | |
index: 1, | |
item_brand: 'conversiontracking.io', // Add item brand if available | |
item_category: '', // Add item category if available | |
item_variant: orderItems.variant_name, | |
price: orderItems.price / 100, // Assuming the price is in cents | |
quantity: orderItems.quantity | |
}] | |
} | |
}); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found at:
https://conversiontracking.io/blog/lemon-squeezy-google-analytics-google-ads-facebook-pixel
https://www.youtube.com/watch?v=f5J-2x9hXVQ