2016-08-24 42 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標記管理器和數據層。這是更新的方式,爲您的網站提供更多,更好,更好的性能。

相關問題