Product view event causes a page view of a product to be counted twice

So I want to track a product view event:

// Push Product View Data to Matomo - Populate parameters dynamically
_paq.push(['setEcommerceView',
    "0123456789", // (Required) productSKU
    "Ecommerce Analytics Book", // (Optional) productName
    "Books", // (Optional) categoryName
    9.99 // (Optional) price
]);

// You must also call trackPageView when tracking a product view 
_paq.push(['trackPageView']);

The problem is, once someoen visits my product page let’s say product-1 then the normal ```
_paq.push([‘trackPageView’]);

which is in the initialization code gets called, and then the ecommerce view event requires also the track page view to be called, this causes duplicate events for the product page view e.g. I see them in logs as if they are visited twice...

How to fix this?

@fahir Did you ever find a solution for this?

@refueled yes, you basically need to structure your code where you have a product view event to not call the track page view.

I made it so that the initialization code calls track page view on every page except product pages, I did that by using url contains and have something unique for the product page.

it’s stupid that it works like that but it is what it is.

Thank you.

Yes, it’s rather annoying it works like that.

For me, I’m using Shopify, so I just do a url check

if (!window.location.pathname.includes('/products/') && !window.location.pathname.includes('/collections/')) { }

@refueled How to do extract the product information on shopify when you use the url check as the condition?

@fahir I am using Shopify’s Customer Events and Web Pixels API.

So my code looks something like this:

analytics.subscribe("product_viewed", (event) => {

  const v = event.data.productVariant ?? {};
  const skuOrId   = v.sku    ?? v.id    ?? 'Unknown SKU';
  const title     = v.product?.title  ?? 'Unknown Product';
  const category  = v.product?.type ?? 'Unknown Category';
  const price     = v.price?.amount  ?? 0;
 
  _paq.push(['setEcommerceView',
      skuOrId,
      title,
      category,
      price
  ]);

    const url = event.context?.window?.location?.href ?? '';
    _paq.push(['setCustomUrl', url]);   
    _paq.push(['enableHeartBeatTimer']);
    _paq.push(['trackPageView']);

});


analytics.subscribe("collection_viewed", (event) => {

  const category = event.data.collection?.title ?? 'Unknown Collection';
  
  _paq.push(['setEcommerceView',
      false,
      false,
      category,
  ]);

    const url = event.context?.window?.location?.href ?? '';
    _paq.push(['setCustomUrl', url]);   
    _paq.push(['enableHeartBeatTimer']);
    _paq.push(['trackPageView']);

});


if (!window.location.pathname.includes('/products/') && !window.location.pathname.includes('/collections/')) {
  
  analytics.subscribe("page_viewed", (event) => {
  
    const url = event.context?.window?.location?.href ?? '';
     
    _paq.push(['setCustomUrl', url]);   
    _paq.push(['enableHeartBeatTimer']);
    _paq.push(['trackPageView']);
  
  });
}