Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text encoding differs in unknown sitations - & vs & #234

Open
henkisdabro opened this issue Aug 16, 2022 · 2 comments
Open

Text encoding differs in unknown sitations - & vs & #234

henkisdabro opened this issue Aug 16, 2022 · 2 comments
Assignees
Milestone

Comments

@henkisdabro
Copy link
Contributor

henkisdabro commented Aug 16, 2022

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
Copy link
Contributor Author

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 henkisdabro changed the title Text encoding differs between certain dataLayer keys Text encoding differs in 'unknown' cases - & vs & Aug 16, 2022
@henkisdabro henkisdabro changed the title Text encoding differs in 'unknown' cases - & vs & Text encoding differs in unknown sitations - & vs & Aug 16, 2022
@henkisdabro
Copy link
Contributor Author

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;
}

@duracelltomi duracelltomi self-assigned this Oct 25, 2022
@duracelltomi duracelltomi added this to the v1.18 milestone Apr 3, 2023
@duracelltomi duracelltomi modified the milestones: v1.18, v1.21 Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants