Але*_*ова 6 javascript php wordpress google-analytics woocommerce
我正在尝试在Google Analytics中添加有关订单的信息.但统计数据并未显示收到有关订单的信息.该网站不使用在线支付(也许原因与此有关).我用了答案
我在我的主题目录中的functions.php中添加了代码.
add_action( 'woocommerce_thankyou', 'google_analytics_integration', 20 );
function google_analytics_integration(){
?>
<script>
ga('require', 'ecommerce');
<?php
// GET the WC_Order object instance from, the Order ID
$order = wc_get_order( $order_id );
$order_key = $order->get_order_key();
$transaction_id = $order->get_transaction_id(); // Doesn't always exist
$transaction_id = $order_id; // (Or the order key or the transaction ID if it exist)
?>
ga('ecommerce:addTransaction', {
'id': '<?php echo $transaction_id; // To be checked ?>',
'affiliation': '<?php echo 'UA-130000602-1'; // replace by yours ?>',
'revenue': '<?php echo $order->get_total(); ?>',
'shipping': '<?php echo $order->get_shipping_total(); ?>',
'tax': '<?php echo $order->get_total_tax(); ?>',
'currency': '<?php echo get_woocommerce_currency(); // Optional ?>'
}); <?php
// LOOP START: Iterate through order items
foreach( $order->get_items() as $item_id => $item ) :
// Get an instance of the WC_Product object
$product = $item->get_product();
// Get the product categories for the product
$categories = wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'names' ) );
$category = reset($categories); // Keep only the first product category
?>
ga('ecommerce:addItem', {
'id': '<?php echo $transaction_id; ?>',
'name': '<?php echo $item->get_name(); ?>',
'sku': '<?php echo $product->get_sku(); ?>',
'category': '<?php echo $category; ?>',
'price': '<?php echo wc_get_price_excluding_tax($product); // OR wc_get_price_including_tax($product) ?>',
'quantity': '<?php echo $item->get_quantity(); ?>',
'currency': '<?php echo get_woocommerce_currency(); // Optional ?>'
});
<?php
endforeach; // LOOP END
?>
ga('ecommerce:send');
</script>
<?php
}
Run Code Online (Sandbox Code Playgroud)
我将计数器代码添加到header.php中
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-130000602-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-130000602-1');
</script>
Run Code Online (Sandbox Code Playgroud)
小智 0
也许这个页面会有所帮助:
另外,看不到上一行的owl定义位置,但看起来它是从$()元素 ID 的 jQuery 定义的。也许在调用 .owlCarousel() 之前检查它是否存在,并且 owlCarousel 没有延迟并且确实附加到您引用的元素。
| 归档时间: |
|
| 查看次数: |
239 次 |
| 最近记录: |