Analytics & Conversion Tracking
The Hytiva Embed Code provides an event listener for tracking activity and conversion reliably, and with respect for customer privacy.
Adding the Event Listener
To begin listening for events, simply modify the configuration object in your Hytiva Embed code to add the event listener.
Use the highlighted lines in the following examples to start your solution:
Tips
Don't forget to add the comma ,
after the previous item in the config
structure. 👍
<hytiva-menu id="hytivaMenu" businessId="@@YOUR_EMBED_KEY_HERE@@" @event=""></hytiva-menu>
<script src="https://e.hytiva.com/v1/js/menu.js" defer></script>
<script>
// Get the menu element
let menu = document.getElementById("hytivaMenu");
// Add your event listener
menu.addEventListener("menu-event", (event) => {
let type = event.detail.type;
let data = event.detail.data;
// Print each event that comes in to the browser's console.
console.log(type, data);
});
</script>
<hytiva-menu id="hytivaMenu" businessId="@@YOUR_EMBED_KEY_HERE@@" @event=""></hytiva-menu>
<script src="https://e.hytiva.com/v1/js/menu.js" defer></script>
<script>
// Get the menu element
let menu = document.getElementById("hytivaMenu");
// Add your event listener
menu.addEventListener("menu-event", (event) => {
let type = event.detail.type;
let data = event.detail.data;
/*
* Do something different with each event, using a switch statement.
*/
switch (type) {
case "user.login":
console.log("User logged in with id : " + data.userId);
break;
case "menu.item.detail":
console.log("Customer viewed item", data)
break;
case "cart.item.add":
console.log("Customer added an item to the cart", data);
break;
case "cart.item.remove":
console.log("Customer removed an item from the cart", data);
break;
case "cart.checkout.start":
console.log("Customer started checkout", data);
break;
case "order.complete":
console.log("Customer submitted a " + data.type + " order", data);
break;
default:
// Do nothing for any other events
}
});
</script>
<hytiva-menu id="hytivaMenu" businessId="@@YOUR_EMBED_KEY_HERE@@" @event=""></hytiva-menu>
<script src="https://e.hytiva.com/v1/js/menu.js" defer></script>
<script>
// Get the menu element
let menu = document.getElementById("hytivaMenu");
// Add your event listener
menu.addEventListener("menu-event", (event) => {
let type = event.detail.type;
let data = event.detail.data;
/*
* Handle events with Google Analytics Enhanced Ecommerce (GA4).
* This code is purposefully verbose for readability and changing it to your needs.
*/
switch (type) {
case "user.login":
gtag("event", "login", {});
break;
case "menu.item.detail":
var items = data.map(({ id, title, brand, category, unitPrice, options }) => {
return {
id: id,
name: title,
brand: brand,
category: category,
quantity : 1,
price: unitPrice,
variant: (options !== null) ? Object.values(options).join() : ""
};
});
/*
* Detail views display one or more variants, which we provide in an array.
* Choose the value you want to associate with each detail view here.
* Comment / uncomment the lines for average, minimum, maximum price or decide your own value.
*/
// Average Price
var value = items.reduce((previousValue, item) => previousValue + item.price, 0.00) / items.length;
// Minimum Price
// var value = Math.min(...items.map((item) => item.price));
// Maximum Price
// var value = Math.max(...items.map((item) => item.price));
gtag("event", "view_item", {
currency: "USD",
value: value,
items: items
});
break;
case "cart.item.add":
var items = data.map(({ id, title, brand, category, unitPrice, quantity, options }) => {
return {
id: id,
name: title,
brand: brand,
category: category,
quantity: quantity,
price: unitPrice,
variant: (options !== null) ? Object.values(options).join() : ""
}
});
var value = items.reduce((previousValue, item) => previousValue + (item.price * item.quantity), 0.00);
gtag("event", "add_to_cart", {
currency: "USD",
value: value,
items: items
});
break;
case "cart.item.remove":
var items = data.map(({ id, title, brand, category, unitPrice, quantity, options }) => {
return {
id: id,
name: title,
brand: brand,
category: category,
quantity: quantity,
price: unitPrice,
variant: (options !== null) ? Object.values(options).join() : ""
}
});
var value = items.reduce((previousValue, item) => previousValue + (item.price * item.quantity), 0.00);
gtag("event", "remove_from_cart", {
currency: "USD",
value: value,
items: items
});
break;
case "cart.checkout.start":
var order = data;
var items = order.items.map(({ id, title, brand, category, unitPrice, quantity, options }) => {
return {
id: id,
name: title,
brand: brand,
category: category,
quantity: quantity,
price: unitPrice,
variant: (options !== null) ? Object.values(options).join() : ""
}
});
gtag('event', 'begin_checkout', {
"value": order.subtotal,
"currency": "USD",
"items": items
});
break;
case "order.complete":
var order = data;
var items = order.items.map(({ id, title, brand, category, unitPrice, quantity, options }) => {
return {
id: id,
name: title,
brand: brand,
category: category,
quantity: quantity,
price: unitPrice,
variant: (options !== null) ? Object.values(options).join() : ""
}
});
gtag('event', 'purchase', {
"transaction_id": order.orderId,
"affiliation": "store",
"value": order.subtotal,
"currency": "USD",
"items": items
});
break;
default:
// Do nothing for any other events
}
});
</script>
Now, you can modify the functionality of the onAnalyticsEvent
function to do what you like with each event, or use an example as it is.
Events
When the event listener is added, you will receive the following events:
User Login
user.login
Provides an identifier for the customer logged in. You can use this event to track unique customers, return customers, etc.
{
"type": "user.login",
"data": {
"userId": "FzZ70uhk-UiMH3rYY81b9ZBUGxb8VpcNh6v45btV5n4"
}
}
Menu Interaction
menu.item.detail
This event lets you know when the customer views the details of a menu item, providing information about the product and its variant(s). For example, when a flower item, with multiple weight options is viewed, you will see multiple items returned, each with different option values.
{
"type": "menu.item.detail",
"data": [
{
"id": "493352.348283",
"title": "CBD Blackberry Gummies",
"category": "cbd-only",
"brand": "Wyld",
"sku": "WLD-CBD-BB",
"unitPrice": 15,
"price": 15,
"options": null
}
]
}
{
"type": "menu.item.detail",
"data": [
{
"id": "452168.4237472",
"title": "Melonade",
"category": "hybrid",
"brand": "Cultivata",
"sku": "MEL-H-1g",
"unitPrice": 15,
"price": 15,
"options": { "weight": "1 Gram" }
}, {
"id": "452169.4237473",
"title": "Melonade",
"category": "hybrid",
"brand": "Cultivata",
"sku": "MEL-H-3.5g",
"unitPrice": 35,
"price": 35,
"options": { "weight": "Eighth" }
}
]
}
Cart Activity
cart.item.add
cart.item.remove
These events let you know when an item is added to or removed from the cart, providing information about the product, variant, price, and quantity.
{
"type": "cart.item.add",
"data": {
"id": "566721.740259",
"quantity": 1,
"title": "Ghost Train Haze Shaker",
"category": "sativa",
"brand": "Nature's Chemistry",
"sku": "4493634",
"options": {
"weight": "Quarter"
}
}
}
{
"type": "cart.item.remove",
"data": {
"id": "566721.740259",
"quantity": 1,
"title": "Ghost Train Haze Shaker",
"category": "sativa",
"brand": "Nature's Chemistry",
"sku": "4493634",
"options": {
"weight": "Quarter"
}
}
}
Begin Checkout
cart.checkout.start
Lets you know when the customer begins the checkout process.
{
"type": "cart.checkout.start",
"data": {
"type": "pickup",
"items": [
{
"id": "566721.740259",
"quantity": 1,
"title": "Ghost Train Haze Shaker",
"sku": "4493634",
"brand": "Nature's Chemistry",
"category": "sativa",
"unitPrice": 70,
"price": 70,
"options": {
"weight": "Quarter"
}
},
{
"id": "493366.658224",
"quantity": 2,
"title": "Marionberry Gummies",
"sku": "3119996",
"brand": "Wyld",
"category": "edibles",
"unitPrice": 20,
"price": 20,
"options": null
}
],
"subtotal": 110,
"total": 130.2125
}
}
Order Completion
order.complete
Lets you know when the customer completes an order, providing details about the order, including its type, and the items on the order.
{
"type": "order.complete",
"data": {
"type": "delivery",
"items": [
{
"id": "566721.740259",
"quantity": 1,
"title": "Ghost Train Haze Shaker",
"sku": "4493634",
"brand": "Nature's Chemistry",
"category": "sativa",
"unitPrice": 70,
"price": 70,
"options": {
"weight": "Quarter"
}
},
{
"id": "493366.658224",
"quantity": 2,
"title": "Marionberry Gummies",
"sku": "3119996",
"brand": "Wyld",
"category": "edibles",
"unitPrice": 20,
"price": 20,
"options": null
}
],
"subtotal": 110,
"total": 130.2125,
"orderId": "a183f21"
}
}