Jump to content

variables for Google Tag Manager


Recommended Posts

I'm having trouble getting the order ID on the order-confirmation page. I need it for Adwords conversion tracking in Google Tag Manager. 

 

I entered the code for a data layer for Google Tag Manager as follows.

<!-- Google Tag Manager data layer -->
        <script>
        {literal}
            dataLayer = [{
                {/literal}
                'orderID': '{$order->id}',
                'orderValue': '{$order->total_paid|number_format:2}',
                'currency': '{$currency->iso_code}'
                {literal}
            }];
        {/literal}
        </script>

I've tried all of these, and they're empty:

$order->id

$order->id_order

$order->order_id

$reference_order

$order_reference

$order_id

$id_order

$reference

 

What else can I try? Is there some list of variables I can use?

 

I'm getting the currency and order total fine using the above. Only the order ID is a mystery.

 

Order reference should be fine too, as I have code that turns the order reference into the order ID everywhere.

 

Maybe the problem is that this is executed in the head tag of the page, where the google tag manager is supposed to be? Is the order ID not available until later on down the page?

Edited by TurkeyFish (see edit history)

Share this post


Link to post
Share on other sites
  • 2 weeks later...

I'll try to give an aproach of how to get this to work. I could not achieve to get the 'affiliation', 'brand', 'category', 'variant' and 'coupon' fields dynamic values, but maybe this could help you. Vars are in spanish, but you can change them to any other names (assign it in php and then use the same name in tpl file).
This code use the "pageview" instead of the event method to retrieve data in tag manager.

 

IMPORTANT: This code is for enharced ecommerce. Normal ecommerce should not work. If you want normal eccomerce follow this link:
http://www.initcoms.com/blog/como-utilizar-google-tag-manager-en-prestashop/
(This could be usefull also if you don't know where are the files or how to make the override)

In php file (override OrderConfirmationController.php or submit.php in paypal module):

$order = new Order($this->id_order);
              $cart = new Cart($order->id_cart);
              $productos = $cart->getProducts();
              $this->context->smarty->assign(array(
                    'id_pedido'=> $this->id_order,
                    'total_a_pagar'=> $order->total_paid_tax_incl,
                    'gastos_envio'=> $order->total_shipping_tax_excl,
                    'impuestos'=> ($order->carrier_tax_rate/100) + 1,
                    'cupones' => $cupones,
                    'productos' => $productos
                    ));

 

In tpl file (yourtheme/order-confirmation.tpl or modules\paypal\views\templates\front in paypal module):
 

{literal}
<script>
dataLayer.push({
  'ecommerce': {
    'purchase': {
      'actionField': {
        'id': '{/literal}{$id_pedido}{literal}',                       
        'affiliation': '',
        'revenue': '{/literal}{$total_a_pagar}{literal}',                    
        'tax':'{/literal}{$impuestos}{literal}',
        'shipping': '{/literal}{$gastos_envio}{literal}',
        'coupon': ''
      },
      'products': [{/literal}{foreach from=$productos item=producto name=productos}{literal}
        {                           
        'name': '{/literal}{$producto.name}{literal}',    
        'id': '{/literal}{$producto.id_product}{literal}',
        'price': '{/literal}{$producto.price_wt}{literal}',
        'brand': '',
        'category': '',
        'variant': '',
        'quantity': {/literal}{$producto.quantity}{literal},
        'coupon': ''                            
           }{/literal}{if $smarty.foreach.productos.iteration != $productos|@count}{literal},{/literal}{/if}{literal}
        {/literal}{/foreach}]{literal}
    }
  }
});
</script>

{/literal}

I'll apreciate any improve to my code with the empty dynamic variables. Also I'll post any improvement.
(Sorry about my broken english)
 

Edited by kokonauta (see edit history)

Share this post


Link to post
Share on other sites
  • 1 year later...
On 7/4/2017 at 7:22 PM, TurkeyFish said:

I'm having trouble getting the order ID on the order-confirmation page. I need it for Adwords conversion tracking in Google Tag Manager. 

 

I entered the code for a data layer for Google Tag Manager as follows.


<!-- Google Tag Manager data layer -->
        <script>
        {literal}
            dataLayer = [{
                {/literal}
                'orderID': '{$order->id}',
                'orderValue': '{$order->total_paid|number_format:2}',
                'currency': '{$currency->iso_code}'
                {literal}
            }];
        {/literal}
        </script>

I've tried all of these, and they're empty:

$order->id

$order->id_order

$order->order_id

$reference_order

$order_reference

$order_id

$id_order

$reference

 

What else can I try? Is there some list of variables I can use?

 

I'm getting the currency and order total fine using the above. Only the order ID is a mystery.

 

Order reference should be fine too, as I have code that turns the order reference into the order ID everywhere.

 

Maybe the problem is that this is executed in the head tag of the page, where the google tag manager is supposed to be? Is the order ID not available until later on down the page?


Hi Where did you add this code?


 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More