0

我想在網站上安裝谷歌Analytics(分析)電子商務跟蹤,而我把它安裝在正確的地方,我不確定如何使用我的PHP代碼來動態創建電子商務交易。 ,谷歌提供的示例的js代碼如下所示:從PHP變量轉換爲Javascript的谷歌Analytics(分析)電子商務跟蹤代碼

<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變量,總一個名爲$amount變量等

如何使用這些變量在我的跟蹤代碼中動態地向Google發送正確的信息?

回答

1

如果JavaScript在您的HTML輸出,你可以把PHP的回聲右側管線(假設你使用.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變量到HTML頁面加載寫的JavaScript文件,然後使用這個新的變量(以下transactionID的例子)之前:

<script> 
    var transactionID = <?php echo $ID; ?>; 
</script> 

<script src="js/myscript.js"></script> 
+0

謝謝!這正是我需要的。 – jb37

1
<?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文件做。 注意,美必須具有通用分析,以使事情在這句法工作。

相關問題