Skip to content

Commit

Permalink
analytics fix
Browse files Browse the repository at this point in the history
  • Loading branch information
a1exFindify committed Jul 18, 2024
1 parent 7fde4f2 commit 1819b78
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
28 changes: 26 additions & 2 deletions assets/findify-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* productPageAnalyticsTagSelectorId can be found in findify-head-injector
*/
const productPageAnalyticsTagSelectorId = "findify-analytics-product";

const isProductDetails = () =>
!!document.getElementById(productPageAnalyticsTagSelectorId);

Expand All @@ -18,21 +19,44 @@
const ref = window.document.referrer;
const url = window.location.href;
const page_type = getPageType();
const itemId = document.getElementById(productPageAnalyticsTagSelectorId)?.getAttribute('data-findify-item-id');
const variantId = document.getElementById(productPageAnalyticsTagSelectorId)?.getAttribute('data-findify-variant-item-id');

const sendEvent = (...props) => findify.core.analytics.sendEvent("view-page", ...props)

try {
findify.core.analytics.sendEvent("view-page", {
sendEvent({
width,
height,
ref,
url,
page_type,
item_id: itemId,
variant_item_id: variantId
});
} catch (error) {
console.error("Error sending view-page event", error);
}
};

const updateCartEvent = () => {};
const updateCartEvent = () => {
const cart = document.querySelector('[data-findify-event="update-cart"]');
const cartItems = cart && Array.prototype.slice.call(cart.children);
const items = cartItems.map(item => {
return {
item_id: item.getAttribute('data-findify-item-id'),
variant_item_id: item.getAttribute('data-findify-variant-item-id'),
unit_price: item.getAttribute('data-findify-unit-price'),
quantity: item.getAttribute('data-findify-quantity')
}
}) || [];

try {
findify.core.analytics.sendEvent("update-cart", {line_items: items});
} catch (error) {
console.error("Error sending update-cart event", error);
}
};

setTimeout(() => {
viewPageEvent();
Expand Down
2 changes: 1 addition & 1 deletion assets/findify-product-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const savePosition = (id, page) => {
}

const initOnProductCardClick = (id, page, properties) => {
document.getElementById(id).addEventListener("click", (e) => {
document.querySelector(`.${properties.widget}_${id}`).addEventListener("click", (e) => {
initProductCardAnalytics(id, properties);
savePosition(id, page);
});
Expand Down
2 changes: 1 addition & 1 deletion sections/findify-product-card.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div
id="{{ id }}"
product-id="product-{{ id }}"
class="findify-product-card {{ class }}"
class="findify-product-card {{ class }} {{ widget }}_{{ id }}"
{%if page != blank%}
page="{{ page }}"
{%endif%}
Expand Down
21 changes: 10 additions & 11 deletions snippets/findify-head-injector.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
data-findify-variant-item-id="{{product.selected_or_first_available_variant.id}}"></div>
{% endif %}

{% if cart %}
<div data-findify-event="update-cart">
{% for item in cart.items %}
<div
data-findify-item-id="{{ item.product_id }}"
data-findify-variant-item-id="{{item.variant_id}}"
data-findify-unit-price="{{ item.price | money_without_currency }}"
data-findify-quantity="{{ item.quantity }}"></div>
{% endfor %}
</div>
{% endif %}

<div data-findify-event="update-cart">
{% for item in cart.items %}
<div
data-findify-item-id="{{ item.product_id }}"
data-findify-variant-item-id="{{item.variant_id}}"
data-findify-unit-price="{{ item.price | money_without_currency }}"
data-findify-quantity="{{ item.quantity }}"></div>
{% endfor %}
</div>

<script>
window.findify = {
Expand Down

0 comments on commit 1819b78

Please sign in to comment.