Ronald Bijker
Ronald Bijker

Reputation: 76

How to put values from a GTM variable array in to a tracking pixel?

Is it possible to implement the following tracking pixel through Google Tag Manager:

enter image description here

...by using the following variable array?:

enter image description here

 [
  {
    id: '4009314961126',
    name: 'Schuurschijf k120',
    price: '4.7900',
    quantity: 1,
    category: 'Gereedschap & Werkplaats/Machine toebehoren/Schuurmachines/Delta-
              &Multibladen',
    brand: ''
  },
  {
    id: '4008496270705',
    name: 'Varta knoopcel CR1216 lithium',
    price: '5.5000',
    quantity: 1,
    category: 'Elektra/Energie/Knoopcellen',
    brand: ''
  }
]

Update #1 With the help of @Max I was able to create the JavaScript I needed:

var pixel_url = "http://gethatch.com/iceleads_rest/merch/91487/direct";
for (i = 0; i < products.length; i++) { 
    var product = products[i];
    pixel_url += ";ean="+product.id+";cur=EUR;pr="+product.price+";qty="+product.quantity+";vendor_name="+product.brand+";prod_name="+product.name;
}
var pixel = document.createElement("img");
pixel.src = pixel_url;
pixel.height = 0;
pixel.width = 0;
document.body.appendChild(pixel);

Working example: https://jsfiddle.net/ronaldbijker/a3qLc607/51/

Upvotes: 0

Views: 515

Answers (1)

Max
Max

Reputation: 13334

Something like this should work:

  1. Create a dataLayer variable called {{ecommerce_products}} which reads ecommerce.products from dataLayer.
  2. Create a tag which is triggered on confirmation page with below code
  3. Update pixel_url = as per your needs

Tag code:

var products = {{ecommerce_products}};

for (i = 0; i < products.length; i++) { 
    var product = products[i];
    var pixel_url = "http://gethatch.com/...prod_name="+product.name+"qty="+product.quantity+...
    var pixel = document.createElement("img");
    pixel.src = pixel_url;
    document.body.appendChild(pixel);
}

Upvotes: 2

Related Questions