GTM Data Layer Names containing spaces cause syntax errors
-
In
src/view/frontend/scripts/google-consent-mode-js.phpthis 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_layercontains spaces those are not removed byesc_jsand 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_jsit 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)
The topic ‘GTM Data Layer Names containing spaces cause syntax errors’ is closed to new replies.