GTM Data Layer Names containing spaces cause syntax errors
-
In
src/view/frontend/scripts/google-consent-mode-js.php
this code is used to print out the GTM script:?>
<script<?php echo ! $consent_attribute ? '' : ' data-cookieconsent="' . esc_attr( $consent_attribute ) . '"'; ?>>
window.<?php echo esc_js( $data_layer ); ?> = window.<?php echo esc_js( $data_layer ); ?> || [];
function gtag() {
<?php echo esc_js( $data_layer ); ?>.push(arguments);
}
gtag("consent", "default", {However if
$data_layer
contains spaces those are not removed byesc_js
and make it through causing a syntax error, e.g.:<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<script data-cookieconsent="ignore">
window.test data layer = window.test data layer || [];
function gtag() {
test data layer.push(arguments);
}Instead of using
esc_js
it would be safer to do something like this beforehand:$data_layer = preg_replace( '/[^a-z0-9_]/i', '', $data_layer );
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.