DataLayer and Google Ads Dynamic Remarketing: Why are order.line_items not pushing to array?

Hello!

Why is my forloop not working?

My intention is to capture each line_item on the order success page and push it to an array called rmkt_items

Unfortunately, it comes back null. What am I missing in my code?

Below is the forloop that I am referring to.

var rmkt_items = [];

{% for line_item in order.line_items %}
 var item = {
    'id': {{ line_item.variant.id }},
    'google_business_vertical': 'retail'
  };

   rmkt_items.push(item);
  {% endfor %}

For more context, the code is being used in the dataLayer for Google Ads Dynamic Remarketing. You’ll notice the value ‘google_business_vertical’: ‘retail’. That will be important for the remarketing campaigns that we create.

Below is the forloop in the dataLayer.

{% if first_time_accessed %}

{% endif %}

I figured it out! I forgot to put a quote around {{ line_item.variant.id }} object that’s located in the declared variable rmkt_items

The corrected code is below.

{% if first_time_accessed %}

{% endif %}

Hi All!

It looks like I was wrong. I didn’t resolve the issue in my previous reply, but I now have.

Instead of using the order.line_items array within the loop, use the line_items array. To clean up the code, I also used let rmkt_items instead of var rmkt_items, and added in the json filter to the line_item.variant.id

With this new clean up code, I have been able capture all the line items on the order success page into the dataLayer.

Below is the corrected code:

{% if first_time_accessed %}

{% endif %}