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)
Link to comment
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)
Link to comment
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?


 

Link to comment
Share on other sites

  • 2 years later...
On 3/17/2022 at 4:44 PM, Pablus said:

With the information of this post I have made it works, finally the data layer is receiving the information needed, thank you!

Sin título.jpg

Hello Pablus, can you share how you got to make it work? 

I mean, what variables did you use?

Thanks for sharing.

Link to comment
Share on other sites

12 minutes ago, Pablus said:

Override OrderConfirmationController.php and create your variables before the line:

        $this->setTemplate(_PS_THEME_DIR_.'order-confirmation.tpl');

in the example, my vars are:

 

            /* GOOGLE TAG MANAGER – TRANSACCIONES */
            $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,
                            'productos' => $productos
            ));
            /* GOOGLE TAG MANAGER – TRANSACCIONES */


then add the script in your theme order-confirmation.tpl between {literal} {/literal}

<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
    'transactionId': '{/literal}{$id_pedido}{literal}',
    'transactionTotal': {/literal}{$total_a_pagar}{literal},
    'transactionTax': {/literal}{$impuestos}{literal},
    'transactionShipping': {/literal}{$gastos_envio}{literal},
    'transactionProducts': [{/literal}{foreach from=$productos item=producto name=productos}{literal}
   {
'sku': '{/literal}{$producto.id_product}{literal}',
'name': '{/literal}{$producto.name}{literal}',
'price': {/literal}{$producto.price_wt}{literal},
'quantity': {/literal}{$producto.quantity}{literal}
}{/literal}{if $smarty.foreach.productos.iteration != $productos|@count}{literal},{/literal}{/if}{literal}
{/literal}{/foreach}],{literal}
'event': 'transactionComplete'
})
</script>

 

Variables names can be changed, only kep the same mane in both files
 

Thanks a lot.

One last questions, I see the variables are in Spanish, will it work if my site is in English? 

If not, can you do me the huge favour of giving the variables in English. Wi really do appreciate.

Thanks once again.

Link to comment
Share on other sites

On 3/21/2022 at 10:11 AM, Mathew Nwaneri said:

Awesome, Thanks a lot.

Have a great day.

 

Matthew I hope you have received the solution of your problem, because my main account was banned for helping you with your doubt and my messages erased. That's how things works here

Edited by BS TEAM (see edit history)
Link to comment
Share on other sites

1 minute ago, BS TEAM said:

Matthew I hope you have received the solution of your problem, because my main account was banned for helping you with your doubt and my messages erased. That's how things works 

That be crazy, I don't even think your solution was against the community policy.

I can still see your messages and solutions, also you are able to post so it seems very strange.

If this is the case, I feel so very sorry for this mishap, its not normal somebody should be banned when contributing to the community.

Yes once agin, thanks so much for your assistance.

Wish you the best.

 

Link to comment
Share on other sites

5 minutes ago, BS TEAM said:

Matthew I hope you have received the solution of your problem, because my main account was banned for helping you with your doubt and my messages erased. That's how things works 

yes you are Right, I see now you posted with another account and your previous account is no longer in the thread.

I am sure this must be an error and I do hope it will be resolved soonest.

I will also write the forum admin to understand why you were banned for contributing to the community.

Keep you posted as soon as I receive (if) their response.

Have a great.

  • Like 1
Link to comment
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...