1

I am using the Business Catalyst system (which uses Liquid Markup/JSON) and I am trying to set up eCommerce Google Analystics Tracking on it. I am also using JS Cookies to store inforamtion.

This code needs to be added to the 'Thank You' / 'Receipt' page. Unfortunately, Business Catalyst doe not have any JSON available for each item that has been purchased on the Receipt page...

Therefore I am trying to store using .set() the G.A. tracking script on the Checkout page and then retrieve it on the receipt page using .get().

Which brings me to my issue, I need to store the following script in a cookie and then retrieve it later. I think it has something to do with stringify-ing the G.A. script and then parsing it later, but that is where my knowledge runs out.

CODE ON CHECKOUT PAGE

I want to store the information in a cookie on this page.

<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 RECEIPT PAGE

I want to retrieve the information from the cookie on this page so I can send it off to Google!

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

Let me know if there is anything missed and thanks!

JHair
  • 194
  • 2
  • 11
  • 1
    _“I need to store the following script in a cookie”_ – you don’t need to put the whole script into the cookie; what you _need_ is the data, because that is the only dynamic part. Btw., instead of a cookie I’d rather recommend using sessionStorage for stuff like this. – CBroe Aug 24 '16 at 10:12
  • @CBroe Thanks for pointing me in the right direction, I'll check it out – JHair Aug 24 '16 at 10:21

2 Answers2

2

This is how I set up eCommerce tracking:

{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>

Just copy paste this code to your thank you page and you are good to go.

Make sure you have

ga('require', 'ec');

in the main part of your code.

Anything about Google Analytics implementation - just ask.

Daut
  • 2,537
  • 1
  • 19
  • 32
  • Forgot to thank you for this, this was a life saver at the time! I had to modify it a little but it is spot on for what I needed. Thank you. I'm not even sure how you found this info out, I can't find anything quite like this in the BC docs. EDIT: Found it - [module_data](http://docs.businesscatalyst.com/developers/liquid/consuming-apis-in-the-front-end-using-module_data) – JHair Oct 10 '16 at 10:01
  • Check this out http://docs.businesscatalyst.com/developers/apps/bc-api-discovery it has saved me hours and hours of coding. – Daut Oct 11 '16 at 13:21
0

As already mentioned you do not store JSON into cookies etc for Google tracking. You cannot store JavaScript or JSON to a cookie, not designed for this anyway. You would also run into issues with the different domains anyway. So ignore all that thinking.

The above is for the older universal analytics but you should check out Google tag manager and the data layer. This is the newer way to do all this and MUCH MUCH more and way better and better performance for your websites.

Shaohao
  • 3,471
  • 7
  • 26
  • 45
thenexus00
  • 22
  • 5