Skip to content Skip to sidebar Skip to footer

Use Php In Script To Send Data Too Google Tag Manager

So right now I am doing something like what is below. What I want to be able to do is pass data dynamically to GTM.. I know I am doing it wrong but I need some help in figuring out

Solution 1:

It seems that you just missed the ' on your json object.

dataLayer.push({
    'transactionId': '<?phpecho$order['id']; ?>',
    'transactionTotal': '<?phpecho  number_format($order['subtotal'],2) ?>',
    'transactionProducts': [{
        'sku': '',
        'name': '<?phpecho$order['programName']; ?>',
        'price': 'here',
        'quantity': 'here'
        }],
    'event' : 'OrderComplete'
});

The rest is fine.

If you got no values for those field, let them be empty by writing an empty string ''.

<?phpfunctionmyProducts() {
        $result = array();
        foreach($productsas$product) {
            array_push($result, "{
                'sku': '" . $product['sku'] . "',
                'name': '" . $product['name'] . "',
                'price': '" . $product['price'] . "',
                'quantity': '" . $product['quantity'] . "'
            }");
        };

        return implode(",", $result);
    }
?>

dataLayer.push({
    'transactionId': '<?phpecho$order['id']; ?>',
    'transactionTotal': '<?phpecho  number_format($order['subtotal'],2) ?>',
    'transactionProducts': [<?phpecho myProducts()?>],
    'event' : 'OrderComplete'
});

Post a Comment for "Use Php In Script To Send Data Too Google Tag Manager"