我在网站上使用Stripe有两个按钮,我想跟踪电子商务转换和按钮点击,但我不知道如何将分析中的代码与我的
HTML页面集成,因为购买时没有确认页面,我不确定按钮操作标记是什么:
<form action="/charge.PHP" method="POST"> <input type='hidden' name='productName' value='1_device'> <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="XXXYYYZZZ" data-image="https://stripe.com/img/documentation/checkout/marketplace.png" data-name="Single" data-description="$1.99 monthly" data-panel-label="Subscribe" data-label="Single" data-amount="199" data-allow-remember-me="false"> </script> </form> <form action="/charge.PHP" method="POST"> <input type='hidden' name='productName' value='5_device'> <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="XXXYYYZZZ" data-image="https://stripe.com/img/documentation/checkout/marketplace.png" data-name="Family" data-description="$9.99" data-panel-label="Subscribe" data-label="Family" data-amount="999" data-allow-remember-me="false"> </script> </form>
解决方法
捕获点击事件
<script> document.body.addEventListener("click",function (event) { if (event.target.prevIoUsElementSibling.classList.contains("stripe-button")) { var data = event.target.prevIoUsElementSibling.dataset; } }); </script>
数据
所有数据都在数据集对象中单击后可访问:
/* All data fields data["key"] data["image"] data["name"] data["description"].replace(/\D/g,''); data["panel-label"] data["label"] data["amount"] data["allow-remember-me"] */
使用Google Analytics事务处理数据
所有在一起应该是这样的:
<script> document.body.addEventListener("click",function (event) { if (event.target.prevIoUsElementSibling.classList.contains("stripe-button")) { var data = event.target.prevIoUsElementSibling.dataset; var transactionID = Math.random().toString(36).substr(2,10); var transactionValue = data["description"].replace(/\D/g,''); // set up ga ga('ecommerce:addItem',{ 'id': transactionID,// Transaction ID. required. 'name': data["name"],// Product name. required. 'category': data["label"],// Category or variation. 'price': transactionValue,// Unit price. 'quantity': 1 // Quantity. }); ga('ecommerce:addTransaction',// Transaction ID. required. 'revenue': transactionValue // Grand Total. }); ga('ecommerce:send'); } }); </script>
文档
https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce