问题
So I have a dataLayer created that works just fine when using the console in Chrome:
In Google Tag Manager, this dataLayer doesn't exist. Why?
The code I've used is below:
<script>
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$dataLayer = [];
foreach($items as $item => $values) {
$_product = $values['data']->post;
?>
<?php
$dataLayer[] = [
'itemUnitID' => $_product->ID,
'itemUnitPrice' => get_post_meta($values['product_id'] , '_price', true),
'itemQuantity' => $values[quantity]
];
};
?>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push(<?php echo json_encode($dataLayer); ?>);
</script>
So basically the code is shooting a dataLayer, but I can't use any of those values in Google Tag Manager as GTM doesn't even recognize the dataLayer in the first place. Why is that?
来源:https://stackoverflow.com/questions/40137320/gtm-doesnt-recognize-my-datalayer-but-console-does