Last active
September 25, 2017 11:42
-
-
Save wpmudev-sls/ccd87b9bf4b3cbe895fe465e4e0997af to your computer and use it in GitHub Desktop.
Adds basic feature of Google Tag Manager to MarketPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: MarketPress - Google Tag Manager | |
Plugin URI: https://premium.wpmudev.org/ | |
Description: Adds Google Tag Manager to site | |
Author: Panos Lyrakis @ WPMUDEV | |
Author URI: https://premium.wpmudev.org/ | |
License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_MP_Google_Tag_Manager' ) ) { | |
class WPMUDEV_MP_Google_Tag_Manager { | |
private static $_instance = null; | |
//private static $_gtm_key = 'GTM-XXXXXXX'; | |
private static $_ga_key = 'UA-XXXXXXXXX-X'; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_MP_Google_Tag_Manager(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_filter( 'mp_order/header', array( $this, 'load_tag_manager_head_script' ), 0 ); | |
} | |
public function load_tag_manager_head_script( $html ){ | |
$order = new MP_Order( get_query_var( 'mp_order_id' ) ); | |
//if order not exist, just return false | |
if ( ! $order->exists() || $order->get_meta( 'mp_ga_tracked' ) ) { | |
return $html; | |
} | |
$order = apply_filters( 'mp_ga_ecommerce', $order ); | |
return $this->_ga_send_order( $order ) . $html; | |
} | |
public function _ga_send_order( $order = null ){ | |
if( is_null( $order ) ){ | |
return; | |
} | |
$cart = $order->get_meta( 'mp_cart_info' ); | |
$products = $cart->get_items_as_objects(); | |
if( empty( $products ) ){ | |
return; | |
} | |
ob_start(); | |
?> | |
<!-- Google Analytics --> | |
<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','https://www.google-analytics.com/analytics.js','ga'); | |
ga('create', '<?php echo self::$_ga_key; ?>', 'auto'); | |
ga('send', 'pageview'); | |
ga('require', 'ecommerce'); // Load the ecommerce plug-in. | |
ga('ecommerce:addTransaction', { | |
'id': '<?php echo $order->ID ?>', // Transaction ID. Required | |
'affiliation': '<?php echo get_bloginfo('name'); ?>', // Affiliation or store name | |
'revenue': '<?php echo $order->get_meta( 'mp_order_total' ); ?>', // Grand Total | |
'shipping': '<?php echo $order->get_meta( 'mp_shipping_total' ); ?>', // Shipping | |
'tax': '<?php echo $order->get_meta( 'mp_tax_total' ); ?>' // Tax | |
}); | |
<?php foreach( $products as $product ){ ?> | |
<?php | |
$product = new MP_Product( $product->ID ); | |
$sku = esc_attr( $product->get_meta( 'sku' ) ); | |
$categories = array(); | |
$terms = get_the_terms( $product->ID, 'product_category' ); | |
if( ! empty( $terms ) ){ | |
foreach( $terms as $term ){ | |
$categories[] = $term->name; | |
} | |
} | |
?> | |
ga('ecommerce:addItem', { | |
'id': '<?php echo $product->ID; ?>', // Transaction ID. Required | |
'name': '<?php echo $product->title( false ); ?>', // Product name. Required | |
'sku': '<?php echo !empty( $sku ) ? $sku : $product->ID; ?>', // SKU/code | |
'category': '<?php echo implode( ',', $categories ); ?>', // Category or variation | |
'price': '<?php echo $product->get_price( 'lowest' ); ?>', // Unit price | |
'quantity': '<?php echo $cart->get_item_qty( $product->ID ); ?>' // Quantity | |
}); | |
<?php } ?> | |
ga('ecommerce:send'); // Send transaction and item data to Google Analytics. | |
</script> | |
<?php | |
$ga_ecommerce = ob_get_clean(); | |
add_post_meta( $order->ID, 'mp_ga_tracked', true, true ); | |
return $ga_ecommerce; | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['WPMUDEV_MP_Google_Tag_Manager'] = WPMUDEV_MP_Google_Tag_Manager::get_instance(); | |
}, 10 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment