使用localStorage和Google Analytics电子商务的Datalayer

时间:2015-06-23 10:26:53

标签: javascript google-analytics e-commerce google-tag-manager

我使用localStorage创建了以下数据层。 我们希望这样做以防止额外的开发成本。

  <script>
  dataLayer = [{
      'transactionId': localStorage.getItem("t_transactionID"),
      'transactionAffiliation': localStorage.getItem("t_storename"),
      'transactionTotal':  localStorage.getItem("t_total"),
      'transactionTax': localStorage.getItem("t_tax"),
      'transactionShipping': localStorage.getItem("t_shipping"),
      'transactionProducts': [{
          'sku': localStorage.getItem("i_transactionID"),
          'name': localStorage.getItem("i_productname"),
          'category': localStorage.getItem("i_category"),
          'price': localStorage.getItem("i_unitprice"),
          'quantity': localStorage.getItem("i_quantity")
      }]
  }];
  </script>

我确信数据层已正确填充,因为当我使用tagassistent时,我看到:

  Parsed Data Layer
  transactionId localStorage.getItem("t_transactionID")
  transactionAffiliation    localStorage.getItem("t_storename")
  transactionTotal  localStorage.getItem("t_total")
  transactionTax    localStorage.getItem("t_tax")
  transactionShipping   localStorage.getItem("t_shipping")
  transactionProducts   [{sku:localStorage.getItem("i_transactionID"),name:localStorage.getItem("i_productname"),category:localStorage.getItem("i_category"),price:localStorage.getItem("i_unitprice"),quantity:localStorage.getItem("i_quantity")}]
  dataLayer
  [
    {
      "transactionId": "107411435054374890",
      "transactionAffiliation": "SYNTRA Limburg",
      "transactionTotal": "195",
      "transactionTax": "33.842975206611555",
      "transactionShipping": "0",
      "transactionProducts":     [
        {
          "sku": "107411435054374890",
          "name": "Summer School: Verkopen is fun (basis)",
          "category": "Marketing en sales",
          "price": "195",
          "quantity": "1"
        }
      ]
    }

当我查看Google Analytics调试扩展程序时,我看到变量都是未定义的?

  Initializing Google Analytics.
  analytics_debug.js:9 Loading resource for plugin: ecommerce
  analytics_debug.js:9 Loading script: "http://www.google-analytics.com/plugins/ua/ecommerce.js"
  analytics_debug.js:9 Running command: ga("create", "", {name: "gtm1435054433247", allowLinker: false})
  analytics_debug.js:9 Creating new tracker: gtm1435054433247
  analytics_debug.js:9 Running command: ga("gtm1435054433247.set", "&gtm", "GTM-N3HVZR")
  analytics_debug.js:9 Running command: ga("gtm1435054433247.set", "hitCallback", [function])
  analytics_debug.js:9 Running command: ga("gtm1435054433247.require", "displayfeatures", undefined, {cookieName: "_dc_gtm_"})
  analytics_debug.js:9 Set called on unknown field: "dcLoaded".
  analytics_debug.js:9 Plugin "displayfeatures" intialized on tracker "gtm1435054433247".
  analytics_debug.js:9 Running command: ga("gtm1435054433247.send", "pageview")
  analytics_debug.js:9 
  Sent beacon:
  v=1&_v=j37d&a=1857107051&t=pageview&_s=1&dl=http%3A%2F%2Fwww.syntra-limburg.be%2Finschrijving%2Fsuccess%2F10398%2F1435054431.79&ul=n…id=&cid=1222114262.1433509428&tid=-1&gtm=GTM-N3HVZR&z=138757632


  analytics_debug.js:9 <unknown>        (&gtm) 
  analytics_debug.js:9 _j1              (&jid) 
  analytics_debug.js:9 adSenseId        (&a)   1857107051
  analytics_debug.js:9 apiVersion       (&v)   1
  analytics_debug.js:9 clientId         (&cid) 1222114262.1433509428
  analytics_debug.js:9 encoding         (&de)  UTF-8
  analytics_debug.js:9 flashVersion     (&fl)  18.0 r0
  analytics_debug.js:9 hitType          (&t)   pageview
  analytics_debug.js:9 javaEnabled      (&je)  1
  analytics_debug.js:9 language         (&ul)  nl
  analytics_debug.js:9 location         (&dl)  http://www.syntra-limburg.be/inschrijving/success/10398/1435054431.79
  analytics_debug.js:9 screenColors     (&sd)  24-bit
  analytics_debug.js:9 screenResolution (&sr)  1920x1080
  analytics_debug.js:9 title            (&dt)  Inschrijven | SYNTRA Limburg | Uw opleiding, onze zaak
  analytics_debug.js:9 trackingId       (&tid) 
  analytics_debug.js:9 viewportSize     (&vp)  1252x927
  analytics_debug.js:9 Running command: ga("create", "", {name: "gtm1435054433249"})
  analytics_debug.js:9 Running command: ga("gtm1435054433249.set", "&gtm", "GTM-N3HVZR")
  analytics_debug.js:9 Running command: ga("gtm1435054433249.set", "hitCallback", [function])
  analytics_debug.js:9 Running command: ga("gtm1435054433249.require", "ecommerce", "//www.google-analytics.com/plugins/ua/ecommerce.js")
  analytics_debug.js:9 Waiting on require of "ecommerce" to be fulfilled.
  analytics_debug.js:9 Executing Google Analytics commands.
  analytics_debug.js:9 Registered new plugin: ga(provide, "ecommerce", Function)
  analytics_debug.js:9 Running command: ga("gtm1435054433249.require", "ecommerce", "//www.google-analytics.com/plugins/ua/ecommerce.js")
  analytics_debug.js:9 Plugin "ecommerce" intialized on tracker "gtm1435054433249".
  analytics_debug.js:9 Running command: **ga("gtm1435054433249.ecommerce:addTransaction", {id: undefined, affiliation: undefined, revenue: undefined, shipping: undefined, tax: undefined})
  analytics_debug.js:9 Running command:** ga("gtm1435054433249.ecommerce:send")

发生了什么事?

数据层和常规Google Analytics代码会在“1 Pageview”上触发。 On 2. DOM Ready我启动了Google Analytics电子商务跟踪代码(来自tagmanager的交易代码)。

https://www.youtube.com/watch?v=ZKjlIhFJMCU 我使用上面的视频作为设置标签和触发器的指南。我不会根据事件触发,而是基于URL(如果它包含成功一词)。

有人可以帮我这个吗?我认为这与首先触发哪个标签有关,但这似乎是正确的,所以这很奇怪。

我从上面删除了我的UA和tagmanager代码。

1 个答案:

答案 0 :(得分:3)

我通过推送修复了我的数据层,而不仅仅是定义它。

<script>

  dataLayer.push({
    'transactionId'          : localStorage.getItem("t_transactionID"),
    'transactionAffiliation' : localStorage.getItem("t_storename"),
    'transactionTotal'       : localStorage.getItem("t_total"),
    'transactionTax'         : localStorage.getItem("t_tax"),
    'transactionShipping'    : localStorage.getItem("t_shipping"),
    'transactionProducts'    : [{
      'sku'      : localStorage.getItem("i_transactionID"),
      'name'     : localStorage.getItem("i_productname"),
      'category' : localStorage.getItem("i_category"),
      'price'    : localStorage.getItem("i_unitprice"),
      'quantity' : localStorage.getItem("i_quantity")
    }],
    'event'                  : 'doneWithDataLayerPush'
  });

</script>

当推送数据层时,我添加了用于触发GA事务标记的事件doneWithDataLayerPush。这样,只有在数据层就绪时才会被触发。