How to get the discount code in the script on the payment page?

Help who can…

To send order data to the Google Tag Manager I add the following script in the settings of the Checkout section:

{% if first_time_accessed %}> dataLayer.push({> event: “purchase”,> ecommerce: {> transaction_id: “{{ order.order_number || order.name }}”,> value: {{ total_price | times: 0.01 }},> tax: {{ tax_price | times: 0.01 }},> shipping: {{ shipping_price | times: 0.01 }},> currency: “{{ order.currency }}”,> discount: “???”,> items: [> {% for line_item in line_items %}{> item_id: “{{ line_item.product_id }}”,> discount: “???”,> item_name: “{{ line_item.variant.title || line_item.title }}”,> currency: “{{ order.currency || shop.currency }}”,> price: “{{ line_item.final_price | times: 0.01 }}”,> quantity: “{{ line_item.quantity }}”> },> > {% endfor %}> ]> }> });

Everything works well. Except I don’t know how to get coupon information (code). I need to send a discount code in the “discount” field.

Can you please suggest me the object and property. I couldn’t find it in the documentation. Tried experimenting with “discount_applications” but couldn’t get anything from it (I have no experience with with LiquidJS and how show object in console by it)

1 Like

Is this at all even possible? In particular, when the tariff version is not a Plus.

You can use following code for total discount

discount: {{ checkout.discounts_amount | times: 0.01 }}

For item level discount use following code

discount’: {{ line_item.line_level_total_discount | times: 0.01 }}

Yes, it’s possible.
Add this piece of code to your dataLayer push

'coupon': {%- assign disc = "" -%}
                              {%- for disc_temp in checkout.discount_applications -%}
                                    {%- assign disc = disc | append: disc_temp.title -%}
                                    {%- unless forloop.last -%} {%- assign disc = disc | append: ',' -%} {%- endunless -%}
                              {%- endfor -%}
                              '{{ disc }}',

If there will be one discount code applied, it will return one value, e.g. ‘Free’, if multiple codes applied then ‘FreeShip, BlackFriday’

Also, if you are using custom web pixels scripts, you can do this way in your script:

analytics.subscribe("checkout_completed", event => {    
   const discount_titles = checkout.discountApplications.map(discount => discount.title);
  const combinedDiscountTitle = discount_titles.join(', ');
//...
   window.dataLayer.push({
 //...push smth
     'coupon': combinedDiscountTitle