gtm4wp icon indicating copy to clipboard operation
gtm4wp copied to clipboard

Text encoding differs in unknown sitations - & vs &

Open henkisdabro opened this issue 1 year ago • 2 comments

I've run into a situation where values fetched from the item_category field and item_brand handles character encoding differently. I'm keen to get some feedback on why this happens and potential solutions.

In Woocommerce I have this category called "Food & Beverage": image

When it's surfaced in the item_category it outputs correctly: image

But when that same category value is outputted into the item_brand field it shows up like this: image

item_brand is populated with the category as I have that setting enabled in GTM4WP: image

henkisdabro avatar Aug 16 '22 05:08 henkisdabro

In fact, when diving deeper, it seems to happen inside the item_category alone, see this excerpt from GA4:

image

I also tested adding secondary dimensions such as Device, Browser Language, Country to the above report to try to see whether the problem is pertaining only to specific dimensions, but that was not the case. Hence I wonder what might cause the character encoding to be different.

I have checked that there is only one Category with this name inside WooCommerce, and the different spelling even occurs on the same page path and same product in certain cases.

henkisdabro avatar Aug 16 '22 05:08 henkisdabro

I created a Custom Javascript variable inside GTM that solves the above matter for me, but I'm not sure it's the best way to do it. It would be insightful to know what causes this encoding inconsistency to happen (in some cases, not all), and resolve it on the plugin side right away, but for now this solution works for me, here's the GTM code to the Custom JS variable in case it helps someone else. Ensure to adjust the {{ecommerce.items}} variable name to match the variable within GTM that returns the ecommerce.items array.

function () {
  var ecommerceItems = {{ecommerce.items}};
  var asString = JSON.stringify(ecommerceItems);
  var encodeAmpersands = asString.replace(/&/g, '&');
  var resultAsJSON = JSON.parse(encodeAmpersands);
  return resultAsJSON;
}

henkisdabro avatar Aug 23 '22 01:08 henkisdabro