页面加载后,将项目添加到同一Datalayer对象

发布于 2025-01-28 10:51:56 字数 910 浏览 4 评论 0原文

在我的网站上,我有一个页面对象,其中包含有关该页面的多个信息,这些信息当前正在查看,这些信息我想将其推向Datalayer。 (标题,描述,页面类型等)。

这些信息中的大部分都可以在页面加载中获得,但是只有在AJAX请求之后才有一条信息。 当该对象多次在页面上可用并让GTM识别它是同一对象并在内部组合时,是否可以将此对象作为不完整对象将其推向数据层?

我想做这样的事情:

<!-- within <head> (available on page load)-->
dataLayer.push({
    "page":{
        "title": "my_page_title",
        "description": "my_page_description"
    });

(Page completes rendering)

<!-- part of AJAX success response -->
dataLayer.push({
    "page":{
        "purchasable": true
    });

并让GTM解释它与我要做的一样:

dataLayer.push({
    "page":{
        "title": "my_page_title",
        "description": "my_page_description",
        "purchasable": true
    });

当前,我将完整的页面对象推向AJAX响应后,将完整的页面对象推向数据层,但是我有一个请求尝试移动所有内容到页面加载。移至页面负载更加复杂,因此我试图看看这是否可以作为替代解决方案。

在GTM中,我们有适当的标签将此页面对象映射到自定义尺寸。我不属于执行此配置的团队的一员,因此我没有太多有关该部分的信息。

On my site, I have a page object that contains multiple bits of information about the page that is currently being viewed that I want to push to the datalayer. (Title, description, page type, etc).

Most of this information is available on page load however there is a piece of information that is only available after an ajax request.
Is it possible to push this object to the datalayer as an incomplete object when it is available on the page multiple times and have GTM recognize that it is the same object and combine it internally?

I want to do something like this:

<!-- within <head> (available on page load)-->
dataLayer.push({
    "page":{
        "title": "my_page_title",
        "description": "my_page_description"
    });

(Page completes rendering)

<!-- part of AJAX success response -->
dataLayer.push({
    "page":{
        "purchasable": true
    });

and have GTM interpret it the same as if I were to do:

dataLayer.push({
    "page":{
        "title": "my_page_title",
        "description": "my_page_description",
        "purchasable": true
    });

Currently, I am pushing the complete page object to the datalayer after the ajax response but there is a request to me to try and move everything to the page load. Moving to page load is more complicated so I'm trying to see if this would be possible as an alternative solution.

Within GTM, we have tags in place to map this page object to a custom dimension. I'm not part of the team that does this configuration though so I don't have much information regarding that part.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涫野音 2025-02-04 10:51:56

当您创建“ 2” DATALAYER变量时,GTM会在其内部数据模型中自动合并对象。

因此,您可以创建一个访问密钥page的数据层变量。在您的代码中,您可以在所有页面数据可用时发出自定义事件:

// Page Load
dataLayer.push({
  page: {
    title: "my_page_title",
  },
});
dataLayer.push({
  event: "page_info",
  page: {
    purchasable: true,
  },
});

当您在自定义事件上触发标签page_info,引用datalayer varible dlv -page,它将可以访问组合的对象。

{
  title: "my_page_title",
  purchasable: true
}

您可以使用GTM调试模式对此进行测试。

The GTM merges objects automatically in its internal data model, when you create a "Version 2" DataLayer Variable.

Hence, you could create a DataLayer variable that accesses the key page. In your code you can emit a custom event when all the page data is available:

// Page Load
dataLayer.push({
  page: {
    title: "my_page_title",
  },
});
dataLayer.push({
  event: "page_info",
  page: {
    purchasable: true,
  },
});

When you trigger a tag on your custom event page_info that references the dataLayer variable DLV - page, it will have access to the combined object.

{
  title: "my_page_title",
  purchasable: true
}

You can use the GTM debug mode to test this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文