为 Google Analytics 电子商务跟踪代码将变量从 PHP 转换为 Javascript
Converting variables from PHP to Javascript for Google Analytics e-Commerce Tracking code
我正在尝试在网站上安装 Google Analytics 电子商务跟踪,虽然我已将其安装在正确的位置,但我不确定如何使用我的 PHP 代码来动态创建电子商务交易。 Google 提供的示例 js 代码如下所示:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
_gaq.push(['_trackPageview']);
_gaq.push(['_addTrans',
'1234', // transaction ID - required
'Acme Clothing', // affiliation or store name
'11.99', // total - required
'1.29', // tax
'5', // shipping
'San Jose', // city
'California', // state or province
'USA' // country
]);
// add item might be called for every item in the shopping cart
// where your ecommerce engine loops through each item in the cart and
// prints out _addItem for each
_gaq.push(['_addItem',
'1234', // transaction ID - required
'DD44', // SKU/code - required
'T-Shirt', // product name
'Green Medium', // category or variation
'11.99', // unit price - required
'1' // quantity - required
]);
_gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
假设我的交易 ID 是一个名为 $ID
的 PHP 变量,total 是一个名为 $amount
的变量,等等
如何在我的跟踪代码中使用这些变量将正确的信息动态发送到 Google?
如果 Javascript 在您的 HTML 输出中,您可以将 PHP echo 右内联(假设您使用的是 .php
文件而不是 .html
个文件:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
_gaq.push(['_trackPageview']);
_gaq.push(['_addTrans',
<?php echo $ID; ?>, // transaction ID - required
'Acme Clothing', // affiliation or store name
'11.99', // total - required
'1.29', // tax
'5', // shipping
'San Jose', // city
'California', // state or province
'USA' // country
]);
// add item might be called for every item in the shopping cart
// where your ecommerce engine loops through each item in the cart and
// prints out _addItem for each
_gaq.push(['_addItem',
<?php echo $ID; ?>, // transaction ID - required
'DD44', // SKU/code - required
'T-Shirt', // product name
'Green Medium', // category or variation
'11.99', // unit price - required
'1' // quantity - required
]);
_gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
或者如果您的 javascript 在脚本文件中,您可以在加载 javascript 文件之前将 javascript 变量写入 html 页面,然后使用此新变量(在下面的示例中 transactionID
):
<script>
var transactionID = <?php echo $ID; ?>;
</script>
<script src="js/myscript.js"></script>
<?php
$order = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId());
$subtotal = $order->getSubtotal();
$order_id = $order->getId(); //the id of the order
//$order->getIncrementId();//the increment id of the order
$gtotal = $order->getGrandTotal();//grand total of the order
$address = $order->getBillingAddress()->getData();
$city = $address['city'];
$state = $address['region'];
$country = $address['country_id'];
$shippingVal=$order->getShippingAmount();
$taxAmount=$order->getTaxAmount();
?>
<script type="text/javascript">
ga('require', 'ecommerce');
ga('ecommerce:addTransaction', {
'id': '<?php echo $order_id; ?>', // Transaction ID. Required.
'affiliation': '', // Affiliation or store name.
'revenue': '<?php echo $gtotal;?>', // Grand Total.
'shipping': '<?php echo $shippingVal;?>', // Shipping.
'tax': '<?php echo $taxAmount;?>' // Tax.
});
<?php
$items = $order->getAllItems();
$itemcount=count($items);
$name=array();
$unitPrice=array();
$sku=array();
$ids=array();
$qty=array();
foreach ($items as $itemId => $item)
{
$product = Mage::getModel('catalog/product')->load($_item['product_id']);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($category_id);
$category = $_cat->getName();
}
?>
ga('ecommerce:addItem', {
'id': '<?php echo $order_id; ?>', // Transaction ID. Required.
'name': '<?php echo $item->getName(); ?>', // Product name. Required.
'sku': '<?php echo $item->getSku(); ?>', // SKU/code.
'category': '<?php echo $category; ?>', // Category or variation.
'price': '<?php echo $item->getPrice(); ?>', // Unit price.
'quantity': '<?php echo $item->getQtyToInvoice(); ?>' // Quantity.
});
<?php
}
?>
ga('ecommerce:send');
</script>
我想来晚了,但这应该在活动模板的 success.phtml 文件上完成。
请注意,您必须拥有 Universal Analytics 才能使此语法正常工作。
我正在尝试在网站上安装 Google Analytics 电子商务跟踪,虽然我已将其安装在正确的位置,但我不确定如何使用我的 PHP 代码来动态创建电子商务交易。 Google 提供的示例 js 代码如下所示:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
_gaq.push(['_trackPageview']);
_gaq.push(['_addTrans',
'1234', // transaction ID - required
'Acme Clothing', // affiliation or store name
'11.99', // total - required
'1.29', // tax
'5', // shipping
'San Jose', // city
'California', // state or province
'USA' // country
]);
// add item might be called for every item in the shopping cart
// where your ecommerce engine loops through each item in the cart and
// prints out _addItem for each
_gaq.push(['_addItem',
'1234', // transaction ID - required
'DD44', // SKU/code - required
'T-Shirt', // product name
'Green Medium', // category or variation
'11.99', // unit price - required
'1' // quantity - required
]);
_gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
假设我的交易 ID 是一个名为 $ID
的 PHP 变量,total 是一个名为 $amount
的变量,等等
如何在我的跟踪代码中使用这些变量将正确的信息动态发送到 Google?
如果 Javascript 在您的 HTML 输出中,您可以将 PHP echo 右内联(假设您使用的是 .php
文件而不是 .html
个文件:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
_gaq.push(['_trackPageview']);
_gaq.push(['_addTrans',
<?php echo $ID; ?>, // transaction ID - required
'Acme Clothing', // affiliation or store name
'11.99', // total - required
'1.29', // tax
'5', // shipping
'San Jose', // city
'California', // state or province
'USA' // country
]);
// add item might be called for every item in the shopping cart
// where your ecommerce engine loops through each item in the cart and
// prints out _addItem for each
_gaq.push(['_addItem',
<?php echo $ID; ?>, // transaction ID - required
'DD44', // SKU/code - required
'T-Shirt', // product name
'Green Medium', // category or variation
'11.99', // unit price - required
'1' // quantity - required
]);
_gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
或者如果您的 javascript 在脚本文件中,您可以在加载 javascript 文件之前将 javascript 变量写入 html 页面,然后使用此新变量(在下面的示例中 transactionID
):
<script>
var transactionID = <?php echo $ID; ?>;
</script>
<script src="js/myscript.js"></script>
<?php
$order = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId());
$subtotal = $order->getSubtotal();
$order_id = $order->getId(); //the id of the order
//$order->getIncrementId();//the increment id of the order
$gtotal = $order->getGrandTotal();//grand total of the order
$address = $order->getBillingAddress()->getData();
$city = $address['city'];
$state = $address['region'];
$country = $address['country_id'];
$shippingVal=$order->getShippingAmount();
$taxAmount=$order->getTaxAmount();
?>
<script type="text/javascript">
ga('require', 'ecommerce');
ga('ecommerce:addTransaction', {
'id': '<?php echo $order_id; ?>', // Transaction ID. Required.
'affiliation': '', // Affiliation or store name.
'revenue': '<?php echo $gtotal;?>', // Grand Total.
'shipping': '<?php echo $shippingVal;?>', // Shipping.
'tax': '<?php echo $taxAmount;?>' // Tax.
});
<?php
$items = $order->getAllItems();
$itemcount=count($items);
$name=array();
$unitPrice=array();
$sku=array();
$ids=array();
$qty=array();
foreach ($items as $itemId => $item)
{
$product = Mage::getModel('catalog/product')->load($_item['product_id']);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($category_id);
$category = $_cat->getName();
}
?>
ga('ecommerce:addItem', {
'id': '<?php echo $order_id; ?>', // Transaction ID. Required.
'name': '<?php echo $item->getName(); ?>', // Product name. Required.
'sku': '<?php echo $item->getSku(); ?>', // SKU/code.
'category': '<?php echo $category; ?>', // Category or variation.
'price': '<?php echo $item->getPrice(); ?>', // Unit price.
'quantity': '<?php echo $item->getQtyToInvoice(); ?>' // Quantity.
});
<?php
}
?>
ga('ecommerce:send');
</script>
我想来晚了,但这应该在活动模板的 success.phtml 文件上完成。 请注意,您必须拥有 Universal Analytics 才能使此语法正常工作。