How to add GA tracking on MemberPress thank you page after transaction

405 Views Asked by At

I am wondering how I can add a google tracking on MemberPress "Thank You" page, basically after a user had paid a monthly or recurring payment and then it will send the data to google analytics with "the name of the plan which they bought", "the payment amount" and "transaction id".

I got this code but its not triggering anything or maybe this is not the right code? I don't know how to debug MemberPress so I am having problems, anyone please help...

function echo_mepr_tracking_script() {

  if(isset($_GET['membership']) && isset($_GET['trans_num'])) {
    $txn = MeprTransaction::get_one_by_trans_num($_GET['trans_num']);
    
    
    if(isset($txn->id) && $txn->id > 0) {
      $trans_id = $txn->id;
      $total = $txn->total;

      // Update total if recurring
      if($txn->subscription_id > 0) {
        $sub = new MeprSubscription($txn->subscription_id);

        if(isset($sub->id) && $sub->id > 0) {
          $trans_id = $sub->id; // Use subscription ID as the payment id for recurring subscriptions

          //Trial period?
          if($sub->trial) {
            $total = $sub->trial_amount;
          } else {
            $total = $sub->total;
            
          }
          
        }
        
      }

      ?>
        <script>
          var fh5coMeprTxnNum = '<?php echo urlencode($trans_id); ?>';
          var fh5coMeprTxnAmount = '<?php echo floatval($total); ?>';
          fbq('track', 'Purchase', {currency: "USD", value: fh5coMeprTxnAmount});
          ga('require', 'ecommerce');
          ga('ecommerce:addTransaction', {
            'id': fh5coMeprTxnNum,
            'revenue': fh5coMeprTxnAmount
          });
          ga('ecommerce:send');
          alert('yes');
        </script>
      <?php
    }
  }
}
add_action('wp_footer', 'echo_mepr_tracking_script');
1

There are 1 best solutions below

0
On

Google requires some additional variables to be passed for it's ecommerce tracking.

I recently implemented this on a number of MemberPress thank you pages, based on code from this gist but using MemberPress Shortcodes.

If you prefer to implement via a Wordpress action hook then just be sure to include all of the required variables as defined by Google Analytics:

addTransaction: id, total
addItem: name, sku, price, quantity

<script>
    var txnId = '[mepr-ecommerce-tracking]%%txn_id%%[/mepr-ecommerce-tracking]';
    // Check for a valid transaction ID before firing...
    if ( txnId ) { 
        ga('require', 'ecommerce');
        ga('ecommerce:addTransaction', {
            id: '[mepr-ecommerce-tracking]%%txn_id%%[/mepr-ecommerce-tracking]' // Transaction ID,
            affiliation: 'Your Store', // Affiliation or store name
            revenue: '[mepr-ecommerce-tracking]%%total%%[/mepr-ecommerce-tracking]', // Grand Total
            shipping: 0, // Shipping cost (n/a for MemberPress)
            tax: '[mepr-ecommerce-tracking]%%tax_amount%%[/mepr-ecommerce-tracking]' // Tax
        });
        ga('ecommerce:addItem', { 
            id: '[mepr-ecommerce-tracking]%%txn_id%%[/mepr-ecommerce-tracking]', // Transaction ID 
            sku: '[mepr-ecommerce-tracking]%%membership_id%%[/mepr-ecommerce-tracking]', // Subscription ID
            name: '[mepr-ecommerce-tracking]%%membership_name%%[/mepr-ecommerce-tracking]', // Membership Name
            category: '', // Category (n/a for MemberPress)
            price: '[mepr-ecommerce-tracking]%%total%%[/mepr-ecommerce-tracking]', // Unit price
            quantity: 1
        });
        ga('ecommerce:send');
    } 
</script>