【问题标题】:Google Tag Manager Data Layer Variable undefined - ShopifyGoogle 跟踪代码管理器数据层变量未定义 - Shopify
【发布时间】:2020-08-14 17:02:17
【问题描述】:

我正在使用 Shopify 变量尝试通过数据层推送数据,以便在特定页面的架构中使用。我使用了一个片段来做到这一点:

<!-- GTM Product Information via Data Layer -->

{% if product.url contains 'products' %}
{%- assign current_variant = product.selected_or_first_available_variant -%}
  <script>
   window.dataLayer = window.dataLayer || [];
   window.dataLayer.push({
      'gtm_product_url': '{{ product.url }}',
      'gtm_product_title' : '{{ product.title }}',
      'gtm_product_image' : '{{ product.featured_image }}',
      'gtm_product_description' : '{{ product.description | strip_html | strip_newlines | replace: '"', '' }}',
      'gtm_date' : '{{'now' | date: '%Y-%m-%d' }}',
      'gtm_sku' : '{{ current_variant.sku }}'
   });
  </script>
{% endif %}


{% if page.url contains 'pages' %}
  <script>
   window.dataLayer = window.dataLayer || [];
   window.dataLayer.push({
      'gtm_page_title': '{{ page.title }}',
      'gtm_page_published': {{ page.published_at }},
      'gtm_page_image' : {{ page_image | img_url }},
      'gtm_page_description' : {{ page_description }}
   });
  </script>
{% endif %}

我在 Google 跟踪代码管理器中添加了变量。数据很好地显示在页面上(使用 Shopify 变量)。而产品页面实际上是在将数据推送到 GTM。

我的问题在于“页面”。我在 Shopify 的页面上看到了数据,并且我已将变量添加到 GTM 中,但是当我尝试将它们添加到 GTM 时,它们都显示为未定义。

我不确定我做错了什么。任何见解将不胜感激!

【问题讨论】:

    标签: shopify google-tag-manager


    【解决方案1】:

    您的代码工作正常,但如果您没有注意到,您没有将页面部分的值括在引号中。您现有的不带引号的代码抛出

    Uncaught SyntaxError: Unexpected token ':'

    包装引号,它应该可以正常工作。

    {% if page.url contains 'pages' %}
      <script>
       window.dataLayer = window.dataLayer || [];
       window.dataLayer.push({
          'gtm_page_title': "{{ page.title }}",
          'gtm_page_published': "{{ page.published_at }}",
          'gtm_page_image' : "{{ page_image | img_url }}",
          'gtm_page_description' : "{{ page_description }}"
       });
      </script>
    {% endif %}
    

    此外,console.log 值用于测试以查看您是否收到预期值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-20
      相关资源
      最近更新 更多