gtm4wp
gtm4wp copied to clipboard
Text encoding differs in unknown sitations - & vs &
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":
When it's surfaced in the item_category
it outputs correctly:
But when that same category value is outputted into the item_brand
field it shows up like this:
item_brand
is populated with the category as I have that setting enabled in GTM4WP:
In fact, when diving deeper, it seems to happen inside the item_category
alone, see this excerpt from GA4:
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.
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;
}