2016-08-24 47 views
1

我正在使用Business Catalyst系统(使用Liquid Markup/JSON),我正试图在其上设置eCommerce Google Analystics Tracking。我也使用JS Cookies来存储信息。将Javascript存储在Cookie中并稍后检索

此代码需要添加到'谢谢你'/'收据'页面。不幸的是,Business Catalyst doe没有任何JSON可用于在收据页面上购买的每件商品...

因此,我试图使用.set()存储G.A.跟踪脚本在Checkout页面上,然后使用.get()在收据页面上检索它。

这使我想到我的问题,我需要将以下脚本存储在cookie中,然后再检索它。我认为这与加强G.A.有关。脚本,然后解析它,但那是我的知识用完的地方。

CODE ON结帐页面

我想存储在这个页面上的Cookie中的信息。

<script> 
// Store eCommerce items in Cookie 
Cookies.set("GAinfo", " 

      ga('ecommerce:addItem', { 
      'id': '00001', 
      'name': 'Product Name 01', 
      'sku': 'ABCD01', 
      'category': 'Fruit', 
      'price': '0.99', 
      'quantity': '13', 
      'currency': 'GBP' 
      }); 

      ga('ecommerce:addItem', { 
      'id': '00002', 
      'name': 'Product Name 02', 
      'sku': 'ABCD02', 
      'category': 'Vegetables', 
      'price': '1.95', 
      'quantity': '6', 
      'currency': 'GBP' 
      }); 

"); 
</script> 

CODE ON收据页面

我想要从这个页面上的Cookie中的信息,所以我可以把它送上谷歌!

<script> 
    var cGAinfo = Cookies.get('GAinfo'); 
    $('.GAinfo-container').html(cGAinfo); 
</script> 

让我知道是否有任何遗漏,谢谢!

+1

_“我需要将以下脚本存储在cookie中”_ - 您不需要将整个脚本放入cookie中;你需要的是数据,因为那是唯一的动态部分。顺便说一句,我宁可推荐使用sessionStorage来代替cookie。 – CBroe

+0

@CBroe谢谢你指点我正确的方向,我会检查出来 – JHair

回答

2

这是我如何设置电子商务跟踪:

{module_data resource="orders" version="v3" fields="id,shippingPrice,totalPrice,discountCode,discountRate,giftVoucherAmount" resourceId="{tag_orderid}" order="id" collection="trans"} 
{module_data resource="orders" version="v3" subresource="products" resourceId="{tag_orderid}" fields="itemId,productId,catalogueId,units,unitPrice,totalPrice,description,product" order="productId" collection="products"} 


<script> 
    {% for prod in products.items -%} 
    {module_data resource="catalogs" version="v3" fields="name" limit="1" where="\{'products.productId':'{{ prod.product.id }}'\}" order="id" collection="cat"} 
    ga('ec:addProduct', { 
     'id': '{{ prod.product.productCode }}', 
     'name': '{{ prod.product.name }}', 
     'category': '{% for item in cat.items -%}{{ item.name }}{% endfor -%}', 
     'brand': '{{ prod.product.custom1 }}', 
     'variant': '', 
     'price': '{{ prod.totalPrice }}', 
     'quantity': {{ prod.units }} 
    }); 
    {% endfor -%} 
    ga('ec:setAction', 'purchase', { 
     'id': '{{ trans.id }}', 
     'affiliation': '{{ trans.discountCode }}', 
     'revenue': '{{ trans.totalPrice }}', 
     'tax': '0', 
     'shipping': '{{ trans.shippingPrice }}', 
     'coupon': '' // User added a coupon at checkout. 
    }); 
ga('send', 'pageview'); 
</script> 

只需复制到你的感谢页面,你是好去粘贴此代码。

确保你在你的代码的主要部分具有

ga('require', 'ec'); 

任何有关Google Analytics(分析)实现的问题 - 只是问。

+0

忘了谢谢你,这是当时的救星!我不得不稍稍修改它,但它是我需要的东西。谢谢。 我甚至不知道你是如何找到这些信息的,我在BC文档中找不到像这样的东西。编辑:发现它 - [module_data](http://docs.businesscatalyst.com/developers/liquid/consuming-apis-in-the-front-end-using-module_data) – JHair

+0

检查出来http://docs.businesscatalyst .com/developers/apps/bc-api-discovery它为我节省了数小时和数小时的编码。 – Daut

0

如前所述,您不会将JSON存储到用于Google跟踪的Cookie等。 您无法将JavaScript或JSON存储到cookie,而不是为此设计的。无论如何,你还会遇到不同域名的问题。 所以忽略所有的想法。

以上是针对较旧的通用分析,但您应该查看Google标记管理器和数据层。这是更新的方式,为您的网站提供更多,更好,更好的性能。

相关问题