Skip to content

Instantly share code, notes, and snippets.

@mathetos
Created May 19, 2022 13:32
Show Gist options
  • Save mathetos/0e998826ec18fc2f6b4fea8f4f773f0e to your computer and use it in GitHub Desktop.
Save mathetos/0e998826ec18fc2f6b4fea8f4f773f0e to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Impact Radius for WooCommerce
* Description: Output Impact Radius affiliate tracking codes to integrate with WooCommerce
* Author: The Events Calendar
* Author URI: https://evnt.is
* Version: 1.0
*/
add_action( 'init', function () {
if ( ! impact_radius_can_track_user() ) {
return;
}
add_action( 'wp_head', 'impact_radius_universal_tracking' );
add_action( 'tec_body', 'impact_radius_track_customer' );
add_action( 'woocommerce_thankyou', 'impact_radius_track_sale' );
} );
/**
* Output the Impact Radius universal tracking code on every page.
*
* @since release/68.2
*/
function impact_radius_universal_tracking() {
?>
<script type="text/javascript"> (function(a,b,c,d,e,f,g){e['ire_o']=c;e[c]=e[c]||function(){(e[c].a=e[c].a||[]).push(arguments)};f=d.createElement(b);g=d.getElementsByTagName(b)[0];f.async=1;f.src=a;g.parentNode.insertBefore(f,g);})('https://utt.impactcdn.com/A252812-5d79-4f73-8291-9ef979ba0d861.js','script','ire',document,window);</script>
<?php
}
/**
* Sends identity event to Impact Radius
*
* @since release/68.2
*
* Needs to be in <body>
*/
function impact_radius_track_customer() {
$impact_user_data = impact_radius_get_user_data();
?>
<script type="text/javascript">
ire( 'identify', <?php echo wp_json_encode( $impact_user_data ); ?> );
</script>
<?php
}
/**
* Sends trackConversion event to Impact Radius
*
* @since release/68.2
*
* @param int $order_id WooCommerce order ID.
*/
function impact_radius_track_sale( int $order_id ) {
$impact_user_data = impact_radius_get_user_data();
$order = wc_get_order( $order_id );
$discount_total = (float) $order->get_discount_total();
$coupons = null;
// this might _actually_ be used for reducing payouts and not for typical coupons… Not sure, but let's go this route for now
if ( $coupon_codes = $order->get_coupon_codes() ) {
$coupons = implode( ', ', $coupon_codes );
}
// This can be "new" or "existing". Figure out if this is their only order or not
$customer_orders = get_posts( [
'numberposts' => 2, // limit the number of orders we fetch, if we get more than 1, we know they are existing
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() ),
] );
$customer_status = count( $customer_orders ) > 1 ? 'existing' : 'new';
$data = [
'orderId' => $order_id,
'customerId' => $impact_user_data['customerId'],
'customerEmail' => $impact_user_data['customerEmail'],
'customerStatus' => $customer_status,
'currencyCode' => 'USD',
'orderPromoCode' => $coupons,
'orderDiscount' => $discount_total,
'items' => [],
'verifySiteDefinitionMatch' => true,
];
// loop over line items
foreach ( $order->get_items() as $item ) {
$product = $item->get_product();
$sku = $product->get_sku();
$data['items'][] = [
'subTotal' => (float) $item->get_subtotal(),
'category' => impact_get_product_category( $sku ),
'sku' => $sku,
'quantity' => $item->get_quantity(),
'name' => $item->get_name(),
];
}
?>
<script type="text/javascript">
ire( 'trackConversion', 29955, <?php echo wp_json_encode( $data ); ?> );
</script>
<?php
}
/**
* Determine the product category. For now we are just separating between SaaS and Plugin products.
*
* @since release/68.2
*
* @param string $sku
*
* @return string
*/
function impact_get_product_category( string $sku ): string {
$category = 'Plugin';
list( $sku_prefix ) = explode( ':', $sku );
if ( in_array( $sku_prefix, [
'event-aggregator',
'promoter',
'loxi-1',
] ) ) {
$category = 'SaaS';
}
return $category;
}
/**
* Get the current user's email (as a SHA1) and ID, providing blank strings if unknown or not set.
*
* @since release/68.2
*
* @return array The current user ID and SHA1 hash of customer's email.
*/
function impact_radius_get_user_data(): array {
$current_user = wp_get_current_user();
return [
'customerId' => empty( $current_user->ID ) ? '' : $current_user->ID,
'customerEmail' => empty( $current_user->user_email ) ? '' : sha1( $current_user->user_email ),
];
}
/**
* Returns whether or not the user should be tracked.
*
* @since release/68.2
*
* @return bool
*/
function impact_radius_can_track_user(): bool {
if ( defined( 'ENVIRONMENT' ) && 'PRODUCTION' !== ENVIRONMENT ) {
return false;
}
// If the user hasn't accepted cookie tracking, don't track the user.
if ( class_exists( 'Cookie_Notice' ) && ! Cookie_Notice::cookies_accepted() ) {
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment