Created
September 29, 2023 13:51
-
-
Save digisavvy/e5007f29a4f2f4f6905d8d48b66fd7f5 to your computer and use it in GitHub Desktop.
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: STT Tracking for Google Tag Manager | |
* Version: 1.0 | |
* Description: Push transaction details into the GTM Data Layer. | |
* Author: Pablo Tocho, of DigiSavvy | |
* Author URI: https://digisavvy.com | |
* License: GPL-2.0+ | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
* Text Domain: dgs | |
*/ | |
if (!defined('ABSPATH')) { | |
exit; | |
} | |
function memberpress_gtm_purchase_data() { | |
if(isset($_GET['membership']) && isset($_GET['trans_num'])) { | |
$transaction = MeprTransaction::get_one_by_trans_num($_GET['trans_num']); | |
if(isset($transaction->id) && $transaction->id > 0) { | |
$subscription = new MeprSubscription($transaction->subscription_id);; | |
$user = $subscription->user(); | |
$purchase_data = array(); | |
if( $subscription ) { | |
$purchase_data['transaction_coupon'] = 'Any'; | |
if( $transaction->coupon_id > 0 ) { | |
$coupon = new MeprCoupon($transaction->coupon_id); | |
$purchase_data['transaction_coupon'] = $coupon->post_name; | |
} | |
$purchase_data['transaction_total'] = $transaction->total; | |
$purchase_data['transaction_sub_total'] = $transaction->amount; | |
$purchase_data['customer_name'] = $user->first_name . $user->last_name; | |
$purchase_data['customer_email'] = $user->user_email; | |
$purchase_data['customer_membership_name'] = $user->user_login; | |
error_log('Inserting the dataLayer script in the footer'); | |
?> | |
<script> | |
window.dataLayer = window.dataLayer || []; | |
dataLayer.push( | |
{ | |
"memberpressPurchaseData": { | |
"customer_name" : "<?= $purchase_data['customer_name']; ?>", | |
"customer_email" : "<?= $purchase_data['customer_email']; ?>", | |
"transaction_total" : "<?= $purchase_data['transaction_total']; ?>", | |
"transaction_sub_total" : "<?= $purchase_data['transaction_sub_total']; ?>", | |
"transaction_coupon" : "<?= $purchase_data['transaction_coupon']; ?>", | |
} | |
} | |
); | |
</script> | |
<?php | |
//error_log('Coupon data is: '); | |
//error_log( print_r( $coupon, true ) ); | |
//error_log('Transaction data is: '); | |
//error_log( print_r( $transaction, true ) ); | |
} | |
} | |
} | |
} | |
add_action('wp_footer', 'memberpress_gtm_purchase_data'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment