php [会员资格] - 添加分析电子商务

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [会员资格] - 添加分析电子商务相关的知识,希望对你有一定的参考价值。

<?php
/**
* Plugin Name: [Membership] - Add analytics e-commerce
* Plugin URI: https://premium.wpmudev.org/
* Description: Membership add google analytics e-commerce
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

add_action( 'ms_controller_frontend_enqueue_scripts', function( $signup_step, $action, $front_controller ){

	global $post;
	$tracking_code ='UA-xxxxxxx-1';

	if( MS_Model_Pages::MS_PAGE_REG_COMPLETE == $post->post_name ){

		$subscription_id = isset( $_REQUEST['ms_relationship_id'] ) ? absint( intval( $_REQUEST['ms_relationship_id'] ) ) : null;

		if( is_null( $subscription_id ) ){
			return;
		}

		$subscription = MS_Factory::load(
			'MS_Model_Relationship',
			$subscription_id
		);
		$membership = $subscription->get_membership();
		$invoice = $subscription->get_current_invoice();
		$has_been_tracked = get_post_meta( $invoice->id, '__has_been_tracked', true );

		if ( $has_been_tracked || ! $invoice->is_paid() ){
			return;
		}

		update_post_meta( $invoice->id, '__has_been_tracked', true );
		?>

		<script>
		  (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','//www.google-analytics.com/analytics.js','ga');

			ga('create', '<?php echo $tracking_code; ?>', 'auto');
			ga("require", "ecommerce", "ecommerce.js");


			ga('ecommerce:addTransaction', {
			  'id': '<?php echo $invoice->id; ?>',                     						// Transaction ID. Required.
			  'affiliation': '<?php echo esc_attr( get_bloginfo( 'blogname' ) ); ?>',   	// Affiliation or store name.
			  'revenue': '<?php echo $invoice->total; ?>',               					// Grand Total.
			  'shipping': '0',                  											// Shipping.
			  'tax': '<?php echo $invoice->tax_rate; ?>'                     				// Tax.
			});


			ga('ecommerce:addItem', {
			  'id': '<?php echo $invoice->id; ?>',                     						// Transaction ID. Required.
			  'name': '<?php echo $membership->name; ?>',    								// Product name. Required.
			  'sku': '<?php echo $membership->id; ?>',                 						// SKU/code.
			  'category': 'Membership Subscriptions',         								// Category or variation.
			  'price': '<?php echo $membership->price; ?>',                 				// Unit price.
			  'quantity': '1'                   											// Quantity.
			});

			ga('ecommerce:send');

		</script>

		<?php

	}


}, 10, 3 );

以上是关于php [会员资格] - 添加分析电子商务的主要内容,如果未能解决你的问题,请参考以下文章

php [会员资格] - 重新发送欢迎电子邮件

php [会员资格] - 滴滴涕会员倒计时

php 删除OG会员资格

php 查询WooCommerce会员资格和FacetWP

php 查询WooCommerce会员资格和FacetWP

php 查询WooCommerce会员资格和FacetWP