On this page
What this issue is
The data layer is a JavaScript array (window.dataLayer) where your site puts facts about the page and the visitor, such as product names, categories, coupon codes and form values. Tags in Google Tag Manager (GTM) read from it and send the values to GA4, Google Ads, Meta and others.
dataLayer.push({
event: 'view_item',
ecommerce: {
items: [{
item_name: 'Salt & Pepper Set',
item_category: 'Café Essentials',
item_variant: 'Crème'
}]
}
});Why it costs you leads or sales
The direct damage is low, because the conversion itself still fires. The cost is in data quality. The same product can appear as Salt & Pepper Set and Salt & Pepper Set in reports, so its numbers are split across rows. Audience rules, product feeds and conditions that match on names or categories can silently miss.
Encoded in data layer
- item_name: Salt & Pepper Set
- Report shows two rows for one product
- Audience rule on the clean name misses it
Clean in data layer
- item_name: Salt & Pepper Set
- One row with the full totals
- Rules and exports match as expected
Example: one product's revenue shown as two report rows because half the pages push an encoded name.
How to check if your site has it
Inspect the data layer in the browser
Open a product page, open DevTools, go to Console and type dataLayer. Expand the objects and look for &, ', " or odd characters like Ã.
Use GTM Preview modeGoogle Tag Manager
In Tag Manager click Preview, load your site, and open the Data Layer tab for each event. Check item_name, item_category, coupon and form field values.
Check GA4 reports
In GA4 open Reports > Monetization > Ecommerce purchases and look for item names that contain & followed by letters and a semicolon, or accented text that looks garbled.
Test names with special characters
Make sure you have products or forms using &, quotes, accents and non-Latin text, then test each one.
- DatabaseName stored as Salt & Pepper Set
- Template outputText HTML-escaped into the script
- Data layerHolds Salt & Pepper Set
- GA4 / Ads / MetaReceive the encoded name
The error happens before the tag runs, so every platform receives the damaged text.
How to fix it
The root causes are usually one of two things: text is HTML-escaped and then placed inside JavaScript, or the page, database or feed uses a character set other than UTF-8. Fix the source first.
Use wp_json_encode
In your theme or plugin, output data layer values with wp_json_encode() instead of esc_html() or esc_js(), which are meant for HTML and inline attributes.
Decode titles
Titles from get_the_title() can contain entities. Wrap them in html_entity_decode( $title, ENT_QUOTES, 'UTF-8' ) before encoding.
Check the database charset
Confirm DB_CHARSET in wp-config.php is utf8mb4.
Use the json filter
In Liquid, output values with {{ product.title | json }} rather than escape. The json filter produces a valid quoted JavaScript string.
Avoid escape inside script tags
The escape filter turns & into &, which is correct for HTML but wrong inside a data layer push.
Create a decoding Custom JavaScript variable
As a stopgap, create a variable that reads the raw value and decodes it. Use it in your tags instead of the raw Data Layer Variable.
Test in Preview
Confirm the decoded variable shows clean text before publishing.
Check CMS-bound custom code
If you embed CMS fields in custom code, the field output may be HTML-escaped. Decode it in script, or push the value from a data attribute read with textContent.
Serialize with a JSON encoder
Build the data layer object on the server and encode it with json_encode (PHP), JSON.stringify (Node) or your language's equivalent. Do not concatenate strings by hand.
Serve UTF-8
Send Content-Type: text/html; charset=utf-8 and add <meta charset="utf-8"> as the first tag in head.
function() {
var raw = {{DLV - item_name}};
if (typeof raw !== 'string') return raw;
var el = document.createElement('textarea');
el.innerHTML = raw;
return el.value;
}<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push(<?php echo wp_json_encode([
'event' => 'view_item',
'item_name' => html_entity_decode(get_the_title(), ENT_QUOTES, 'UTF-8')
]); ?>);
</script>How to confirm it's fixed
- dataLayer in the console shows plain & and accents for a test product
- GTM Preview Data Layer tab shows clean item_name and item_category
- GA4 DebugView shows the clean value on the event
- New orders in GA4 land on the same product row as older ones
- Test a quote, an ampersand and a non-Latin name
How to prevent it happening again
- Always build the data layer from an object and a JSON encoder, never string concatenation
- Keep one test product with &, quotes and accents in its name
- Re-run the GTM Preview check after theme, plugin or feed changes
- Use UTF-8 everywhere: database, files, headers and meta charset
- Document who owns the data layer code
How FunnelGuard handles this
FunnelGuard does not check this yet. Checking the text of data layer values for entities and encoding errors is a possible future check.
What FunnelGuard does cover nearby is whether your journeys still run and whether the tracking hits for GA4, Google Ads, Meta and other platforms are sent at each step. That tells you a tag stopped firing, but not whether the values inside are cleanly encoded.
To watch for this yourself, keep the special-character test product, check it in GTM Preview after every release, and scan your GA4 item name report for & followed by letters and ; once a month.
Questions people ask
Why does my GA4 item name show & instead of &?
The product name was HTML-escaped before it was placed in the data layer. Output it through a JSON encoder (such as wp_json_encode or Shopify's json filter) instead of an HTML escape function, and decode any entities from the source.
Why are accented characters in my data layer garbled?
Text saved in UTF-8 is being read as another character set, such as ISO-8859-1, so é becomes é. Make sure your database, page headers and meta charset all use UTF-8.
How do I decode HTML entities in Google Tag Manager?
Create a Custom JavaScript variable that sets the value as innerHTML of a temporary textarea element and returns its value. Use that variable in your tags. Treat it as a stopgap and fix the source.
Does an encoded data layer value stop conversions from tracking?
Usually not. The event still fires. The damage is to data quality, such as split product rows and rules that fail to match.
How can I see my data layer values?
Type dataLayer in the browser console, or use Preview mode in Google Tag Manager and open the Data Layer tab for each event.
Should I use esc_html or esc_js for data layer values in WordPress?
No. Those are meant for HTML and inline attributes. Use wp_json_encode() to output the whole data layer object safely.
Can FunnelGuard detect HTML entities in my data layer?
No, not yet. It is a possible future check. FunnelGuard does check that journeys complete and that tracking hits are sent, but not the encoding of the values.
Related issues
- TRK-342Value differs between GA4, Meta and Ads for the same orderFix guide
- TRK-353Prices with thousands separators parsed wronglyFix guide
- TRK-341Value multiplied (cents vs units)Fix guide
- TRK-337Same conversion counted by multiple tags (GA4 + Ads import + Ads tag)Fix guide
- TRK-343Currency hardcoded while store sells in multiple currenciesFix guide
- TRK-338Duplicate purchase across browser and server without dedupFix guide