Skip to content

Instantly share code, notes, and snippets.

@timmyc
Last active March 17, 2020 23:27
Show Gist options
  • Save timmyc/edf820bded9a805a0746751a421bfb8a to your computer and use it in GitHub Desktop.
Save timmyc/edf820bded9a805a0746751a421bfb8a to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Woo Tracks Filter Examples
* Plugin URI: https://woocommerce.com
* Description: Examples of how to filter track properties
* Author: WooCommerce
* Author URI: https://woocommerce.com/
* Domain Path: /languages
* Version: 0.1.0
*/
add_filter( 'woocommerce_apply_tracking', '__return_true' );
/**
* Example of adding a JS-based tracks filter
*/
add_filter( 'admin_footer', 'woocommerce_tracks_filter_example_add_tracks_js_filter' );
function woocommerce_tracks_filter_example_add_tracks_js_filter() {
?>
<!-- WooCommerce JS Tracks Filter -->
<script type="text/javascript">
woocommerceTracksFilterProperties = function( properties, eventName ) {
console.log( "eventName", eventName );
console.log( "properties", properties );
// let's add a host prop for all events.
properties.host = 'isorex';
return properties;
}
if ( window.wp && window.wp.hooks && window.wp.hooks.addFilter ) {
window.wp.hooks.addFilter( "woocommerce_tracks_client_event_properties", "woocommerce", woocommerceTracksFilterProperties );
}
</script>
<?php
}
/**
* Example of adding a props tracks filter for PHP events.
*/
add_filter( 'woocommerce_tracks_event_properties', 'woocommerce_tracks_filter_example_add_tracks_php_filter', 10, 2 );
function woocommerce_tracks_filter_example_add_tracks_php_filter( $properties, $event_name ){
error_log( 'Event Name: ' . $event_name );
$properties['host'] = 'isorex';
$properties['_ui'] = 'nope';
//example of only modifying props on a particular event.
if ( 'wcadmin_orders_view' === $event_name ) {
$properties['amaze'] = 'stuff';
}
error_log( print_r( $properties, true ) );
return $properties;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment