text 将电子商务跟踪代码添加到感谢页面

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 将电子商务跟踪代码添加到感谢页面相关的知识,希望对你有一定的参考价值。

<?php
/**
 * As - Add custom tracking code to the thank-you page
 */
add_action( 'woocommerce_thankyou', 'add_ecommerce_code' );

function add_ecommerce_code( $order_id ) {
	// https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce
	// https://docs.woocommerce.com/document/custom-tracking-code-for-the-thanks-page/

	// Lets grab the order
	$dp = (isset($filter['dp'])) ? intval($filter['dp']) : 2;				        // Get the decimal precession
	$order = wc_get_order( $order_id );												// getting order Object
	$line_items = $order->get_items();

	// Transaction Data
	$trans = array(
				'id'			=> 	$order_id,						// Order ID
				// 'affiliation'	=>	'Acme Clothing',
				'currency'		=> 	$order->get_currency(),
				'shipping'		=>	wc_format_decimal($order->get_total_shipping(), $dp),
				'tax'			=>	wc_format_decimal($order->get_total_tax(), $dp),
				'revenue'		=>	$order->get_total(),			// Order total
			);

	// Prepare array of all items
	$arrIndex = 0;
	foreach ($line_items as $itemArr) {
		$items[$arrIndex]['id'] 		= 	$itemArr['product_id'];
		$items[$arrIndex]['name'] 		= 	$itemArr['name'];
		$items[$arrIndex]['quantity'] 	= 	$itemArr['quantity'];
		$items[$arrIndex]['price'] 		= 	$itemArr['total'];
		$arrIndex++;
	}

	// print_r($trans);
	?>

	<script>
		// Google Analytics
		(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
		(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
		m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
		})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

		//ga('create', 'UA-118631959-1', 'auto');
		ga('create', 'UA-113289544-1', 'auto');
		ga('send', 'pageview');
		// Google Analytics

		ga('require', 'ecommerce');


		<?php
			echo getTransactionJs($trans);

			foreach ($items as &$item) {
				echo getItemJs($trans['id'], $item);
			}
		?>


		ga('ecommerce:send');
	</script>


<?php
}

// Function to return the JavaScript representation of a TransactionData object.
function getTransactionJs(&$trans) {
return <<<HTML
    ga('ecommerce:addTransaction', {
        'id': '{$trans['id']}',
        'currency': '{$trans['currency']}',
        'revenue': '{$trans['revenue']}',
        'shipping': '{$trans['shipping']}',
        'tax': '{$trans['tax']}'
    });
HTML;
}

// Function to return the JavaScript representation of an ItemData object.
function getItemJs(&$transId, &$item) {
return <<<HTML
    ga('ecommerce:addItem', {
        'id': '$transId',
        'name': '{$item['name']}',
        'sku': '{$item['id']}',
        'price': '{$item['price']}',
        'quantity': '{$item['quantity']}'
    });
HTML;
}
/**
 * As - Add custom tracking code to the thank-you page
 */

以上是关于text 将电子商务跟踪代码添加到感谢页面的主要内容,如果未能解决你的问题,请参考以下文章

php 将跟踪代码添加到wp谢谢页面

如何将联系表重定向到感谢页面

Google跟踪代码管理器表单 - 感谢您的页面

php [WooCommerce Core]感谢页面的自定义跟踪代码

php [WooCommerce Core]感谢页面的自定义跟踪代码

text 添加感谢页面并确认页面与联系表格7