jQuery – 循环DOM对象并将它们传递给dataLayer(json?)

huangapple go评论59阅读模式
英文:

jQuery – loop DOM objects and pass them to dataLayer(json?)

问题

I have to manually create my events for GA4, and I have a little knowledge of jQuery.
I'm trying to loop through my DOM elements and pass the data from them to dataLayer (as I understand, that's json format?), that Google wants.
My code looks like that (in simplified form):

function viewCartGA4() {
  let product = $('table.products tbody tr');
  dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
  dataLayer.push({
    event: "view_cart",
    ecommerce: {
      currency: "xxx",
      items: [
      {
        item_name: product.children().data("name"),
        item_id: product.children().data("code")
      }
      ]      
    }
  })
}

If there's only one table row, there's no problem, but how can I pass more than one to an array items required by Google?
Tried to create an $.each function inside an array, to no avail.


I'm not a pro by any means, it just happened, that I cover things like that in my company. Tried to look through the Internet, I found only the opposite situation (to pass json data to the DOM elements/content). I have just the basic understanding of coding and coding concepts. Appreciate the help.

英文:

I have to manually create my events for GA4, and I have a little knowledge of jQuery.
I'm trying to loop through my DOM elements and pass the data from them to dataLayer (as I understand, that's json format?), that Google wants.
My code looks like that (in simplified form):

function viewCartGA4() {
  let product = $('table.products tbody tr');
  dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
  dataLayer.push({
    event: "view_cart",
    ecommerce: {
      currency: "xxx",
      items: [
      {
        item_name: product.children().data("name"),
        item_id: product.children().data("code")
      }
      ]      
    }
  })
}

If there's only one table row, there's no problem, but how can I pass more than one to an array items required by Google?
Tried to create an $.each function inside an array, to no avail.


I'm not a pro by any means, it just happened, that I cover things like that in my company. Tried to look through the Internet, I found only the opposite situation (to pass json data to the DOM elements/content). I have just the basic understanding of coding and coding concepts. Appreciate the help.

答案1

得分: 0

以下是翻译好的部分:

假设提供的代码在单个表行时有效,对于多个行,您需要循环遍历每个行并将其映射到所需的对象。jQuery 有 .map 方法可以使用:

function viewCartGA4() {
  // 将每个表行(tr)映射到一个对象
  const items = $('table.products tbody tr').map(function () {
  	return {
      item_name: $(this).children('[data-name]').data("name"),
      item_id: $(this).children('[data-code]').data("code")
    };
  }).get(); // .get() 将此从 jQuery 包装的数组转换为未包装的数组
  
  dataLayer.push({ ecommerce: null });  // 清除先前的 ecommerce 对象。
  dataLayer.push({
    event: "view_cart",
    ecommerce: {
      currency: "xxx",
      items: items // 分配映射的 `items`
    }
  });
}
英文:

Assuming the supplied code works when there is a single table row, for many rows you would need to loop over each and map it to the desired object. jQuery has .map method that can used:

function viewCartGA4() {
  // map each table row (tr) to an object
  const items = $('table.products tbody tr').map(function () {
  	return {
      item_name: $(this).children('[data-name]').data("name"),
      item_id: $(this).children('[data-code]').data("code")
    };
  }).get(); // .get() turns this from a jQuery wrapped array into an unwrapped one
  
  dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
  dataLayer.push({
    event: "view_cart",
    ecommerce: {
      currency: "xxx",
      items: items // assign the mapped `items`
    }
  });
}

huangapple
  • 本文由 发表于 2023年4月13日 18:07:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定