Skip to content

Commit

Permalink
fix: product list view and product clicked enhancement (#38)
Browse files Browse the repository at this point in the history
* fix: product list view and product clicked enhancement

* chore: small fix for identifier flow

* revert: chore: small fix for identifier flow

* fix: sending rudderIdentifier before cart update call (#39)
  • Loading branch information
anantjain45823 authored Jul 31, 2023
1 parent 2b8fec3 commit 103620b
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions deviceModeInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var rudderTracking = (function () {
const htmlSelector = {};
const pageURL = window.location.href;
let pageCurrency = "";
let pagePathArr = "";
let userId;
let enableClientIdentifierEvents;

Expand Down Expand Up @@ -101,7 +102,6 @@ var rudderTracking = (function () {
const wasUserIdentifiedPreviously = cookie_action({ action: "get", name: "rudder_user_id" }) === "captured";
if (wasUserIdentifiedPreviously) {
rudderanalytics.reset(true);
anonymousIdChanged = true;
cookie_action({
action: "set",
name: "rudder_user_id",
Expand All @@ -114,6 +114,7 @@ var rudderTracking = (function () {
}
function init() {
pageCurrency = Shopify.currency.active;
pagePathArr = window.location.pathname.split("/");
userId = ShopifyAnalytics.meta.page.customerId || __st.cid;
const userLoggedOut = checkIfUserLoggedOut(userId);

Expand All @@ -136,10 +137,12 @@ var rudderTracking = (function () {
.then((cart) => {
const needToUpdateCart = checkCartNeedsToBeUpdated(cart);
if (userLoggedOut || needToUpdateCart) {
updateCartAttribute().then((cart) => {
sendIdentifierToRudderWebhook(cart);
});
console.debug("Successfully updated cart");
sendIdentifierToRudderWebhook(cart);
updateCartAttribute().then(() => {
console.debug("Successfully updated cart");
}).catch((error) => {
console.debug("Error occurred while updating cart:", error);
});;
}
else {
/* Used else condition as it was otherwise sending rudderIdentifier twice
Expand All @@ -148,11 +151,7 @@ var rudderTracking = (function () {
*/
checkAndSendRudderIdentifier();// sending rudderIdentifier periodically after this
}
})
.catch((error) => {
console.debug("Error occurred while updating cart:", error);
});
productListViews();
isClientSideIdentifierEventsEnabled().then(response => {
if (!!response) {
enableClientIdentifierEvents = true;
Expand All @@ -161,10 +160,10 @@ var rudderTracking = (function () {
enableClientIdentifierEvents = false;
}
});

productListViews();
trackPageEvent();
trackNamedPageView();
trackProductListPage();
productListPage();

rs$("button[data-search-form-submit]").on("click", trackProductSearch);
}
Expand Down Expand Up @@ -231,12 +230,14 @@ var rudderTracking = (function () {
function callProductListViewedEvent(productsOnThePage, productConsidered) {
const elementsToSend = [];
const productsToSend = [];
let excludeCurrentPage = false;
if (pagePathArr[pagePathArr.length - 2] == "products") {
excludeCurrentPage = true;
}
productsOnThePage.forEach((element) => {
const isProductVisible = productIsVisible(window, element);
const isProductAlreadyConsidered = productConsidered.includes(
element.href
);
if (isProductVisible && !isProductAlreadyConsidered) {
const isProductAlreadyConsidered = productConsidered.includes(element.href);
if (isProductVisible && !isProductAlreadyConsidered && (!excludeCurrentPage || !element.href.includes(pageURL))) {
elementsToSend.push(element);
productConsidered.push(element.href);
}
Expand Down Expand Up @@ -286,7 +287,6 @@ var rudderTracking = (function () {
document.addEventListener("scroll", () => {
//assumes that people need 200ms after scrolling stops to register an impression
clearTimeout(waitForScroll);

waitForScroll = window.setTimeout(() => {
callProductListViewedEvent(productsOnThePage, productConsidered);
}, 200);
Expand Down Expand Up @@ -440,15 +440,6 @@ var rudderTracking = (function () {
}
}

const trackProductListPage = () => {
const pagePathArr = window.location.pathname.split("/");
// If the url is = /products or /collections/{collectionId}
if (pagePathArr[pagePathArr.length - 2] == "collections") {
// To track product clicked Events
productListPage();
}
}

function trackNamedPageView() {
let name = "",
mappedPageName = "";
Expand Down

0 comments on commit 103620b

Please sign in to comment.