PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / classes / WpMatomo / Ecommerce / MemberPress.php
matomo / classes / WpMatomo / Ecommerce Last commit date
Base.php 6 years ago EasyDigitalDownloads.php 6 years ago MemberPress.php 6 years ago Woocommerce.php 6 years ago
MemberPress.php
130 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 * @package matomo
8 */
9
10 namespace WpMatomo\Ecommerce;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // if accessed directly
14 }
15
16 class MemberPress extends Base {
17
18 public function register_hooks() {
19 if ( ! is_admin() ) {
20 parent::register_hooks();
21
22 add_action( 'template_redirect', array( $this, 'on_product_view' ), 99999, 0 );
23 add_action( 'wp_footer', array( $this, 'on_order' ), 99999, 2 );
24 add_action( 'mepr-signup', array( $this, 'on_cart_update' ), 99999, 1 );
25 }
26 }
27
28 /**
29 * @param \MeprTransaction $transaction
30 */
31 public function on_cart_update( $transaction ) {
32 $tracking_code = '';
33 $sku = $transaction->id;
34 $product = $transaction->product();
35 $params = array(
36 'addEcommerceItem',
37 $sku,
38 $product->post_title,
39 $categories = array(),
40 $transaction->amount,
41 1,
42 );
43 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
44
45 $total = $transaction->total;
46 $tracking_code .= $this->make_matomo_js_tracker_call( array( 'trackEcommerceCartUpdate', $total ) );
47
48 // we can't echo directly as we wouldn't know where in the template rendering stage we are and whether
49 // we're supposed to print or not etc
50 $this->cart_update_queue = $this->wrap_script( $tracking_code );
51 $this->logger->log( 'Tracked ecommerce cart update: ' );
52 }
53
54 public function on_product_view() {
55 if ( ! is_singular( 'memberpressproduct' ) ) {
56 return;
57 }
58
59 $product_id = get_the_ID();
60
61 if ( empty( $product_id ) ) {
62 return;
63 }
64
65 if ( ! class_exists( '\MeprProduct' ) ) {
66 return;
67 }
68
69 $product = new \MeprProduct( $product_id );
70
71 $sku = $product_id;
72
73 $params = array(
74 'setEcommerceView',
75 '' . $sku,
76 $product->post_title,
77 $categories = array(),
78 $product->price,
79 );
80
81 echo $this->wrap_script( $this->make_matomo_js_tracker_call( $params ) );
82 }
83
84 public function on_order() {
85 if ( isset( $_GET['membership'] )
86 && isset( $_GET['trans_num'] )
87 && class_exists( '\MeprTransaction' ) ) {
88 $txn = \MeprTransaction::get_one_by_trans_num( (int) $_GET['trans_num'] );
89 if ( isset( $txn->id ) && $txn->id > 0 ) {
90 if ( $this->has_order_been_tracked_already( $txn->id ) ) {
91 return;
92 }
93 $this->set_order_been_tracked( $txn->id );
94 $transaction = new \MeprTransaction( $txn->id );
95 $order_id_to_track = $txn->trans_num;
96 $product = $transaction->product();
97
98 $discount = 0;
99
100 if ( $product && $transaction->coupon() ) {
101 $discount = $product->price - $txn->amount;
102 }
103 $tracking_code = '';
104 $params = array(
105 'addEcommerceItem',
106 '' . $product->ID,
107 $product->post_title,
108 array(),
109 $txn->amount,
110 1,
111 );
112 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
113 $params = array(
114 'trackEcommerceOrder',
115 '' . $order_id_to_track,
116 $txn->total,
117 $txn->amount,
118 $txn->tax_amount,
119 $shipping = 0,
120 $discount,
121 );
122 $tracking_code .= $this->make_matomo_js_tracker_call( $params );
123
124 echo $this->wrap_script( $tracking_code );
125 }
126 }
127 }
128
129 }
130