Created
April 8, 2014 18:48
-
-
Save carter2422/10170132 to your computer and use it in GitHub Desktop.
Getting a 'false' expiration on Membership Payment events
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
// Track account upgrade via gift redemption | |
function cgc_rcp_track_payment( $payment_id = 0, $args = array(), $amount ) { | |
if( ! function_exists( 'rcp_get_subscription_name' ) ) | |
return; | |
global $wpdb; | |
$mp = Mixpanel::getInstance( CGC_MIXPANEL_API ); | |
$user = get_userdata( $args['user_id'] ); | |
$mp->identify( $user->user_login ); | |
$rcp_payments = new RCP_Payments; | |
// Get the last payment of the user | |
$this_payment = $rcp_payments->get_payment( $payment_id ); | |
$last_payment = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . rcp_get_payments_db_name() . " WHERE `user_id`='%d' AND id < '%d' ORDER BY id DESC LIMIT 1;", $args['user_id'], $payment_id ) ); | |
$renewal = ! empty( $last_payment ); | |
// get expiration | |
$expiration = rcp_get_expiration_date( $user_id ); | |
$person_props = array(); | |
$person_props['$first_name'] = $user->first_name; | |
$person_props['$last_name'] = $user->last_name; | |
$person_props['$email'] = $user->user_email; | |
$person_props['$username'] = $user->user_login; | |
$person_props['Account Type'] = 'Citizen'; | |
$person_props['Account Status'] = 'Active'; | |
$person_props['Expiration'] = $expiration; | |
$person_props['Payment Term'] = rcp_get_subscription( $user->ID ); | |
$person_props['Last Payment Date'] = $this_payment->date; | |
$mp->people->set( $user->user_login, $person_props ); | |
$event_props = array(); | |
$event_props['distinct_id'] = $user->user_login; | |
$event_props['Value'] = $amount; | |
$event_props['Payment Term'] = rcp_get_subscription( $user->ID ); | |
$event_props['Payment Type'] = $renewal ? 'Renewal' : 'Initial'; | |
$event_props['Last Payment Date'] = $this_payment->date; | |
$event_props['Expiration'] = $expiration; | |
$mp->track( 'Membership Payment', $event_props ); | |
$mp->people->trackCharge( $user->user_login, $amount ); | |
} | |
add_action( 'rcp_insert_payment', 'cgc_rcp_track_payment', 10, 3 ); | |
// Track account upgrade | |
function cgc_rcp_account_upgrade( $user_id, $data ) { | |
if( ! function_exists( 'rcp_get_subscription_name' ) ) | |
return; | |
$mp = Mixpanel::getInstance( CGC_MIXPANEL_API ); | |
$user = get_userdata( $user_id ); | |
$mp->identify( $user->user_login ); | |
$subscription = rcp_get_subscription( $user_id ); | |
$expiration = rcp_get_expiration_date( $user_id ); | |
$recurring = rcp_is_recurring( $user_id ) ? 'Yes' : 'No'; | |
$rcp_payments = new RCP_Payments; | |
$new_user = $rcp_payments->last_payment_of_user( $user_id ); | |
$user_time = strtotime( $user->user_registered, current_time( 'timestamp' ) ); | |
$renewal = ! empty( $new_user ); | |
$upgrade = $user_time < $ten_min_ago && ! $renewal ? true : false; | |
$person_props = array(); | |
$person_props['$first_name'] = $user->first_name; | |
$person_props['$last_name'] = $user->last_name; | |
$person_props['$email'] = $user->user_email; | |
$person_props['$username'] = $user->user_login; | |
$person_props['Account Type'] = 'Citizen'; | |
$person_props['Account Status'] = 'Active'; | |
$person_props['Account Level'] = $subscription; | |
$person_props['Recurring'] = $recurring; | |
$person_props['Expiration'] = $expiration; | |
$person_props['$created'] = date( 'Y-m-d H:i:s' ); | |
$mp->people->set( $user->user_login, $person_props, array( '$ip' => cgc_mixpanel_get_ip() ) ); | |
$event_props = array(); | |
$event_props['distinct_id'] = $user->user_login; | |
$event_props['Account Type'] = 'Citizen'; | |
$event_props['Account Status'] = 'Active'; | |
$event_props['Account Level'] = $subscription; | |
$event_props['Recurring'] = $recurring; | |
$event_props['Expiration'] = $expiration; | |
$event_props['Renewal'] = $renewal ? 'Yes' : 'No'; | |
$event_props['Time Since Creation'] = human_time_diff( $user_time, current_time( 'timestamp' ) ); | |
$mp->track( 'Account Upgraded', $event_props ); | |
} | |
add_action( 'rcp_stripe_signup', 'cgc_rcp_account_upgrade', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment