Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericakfranz/d481791053c8eb3c9625 to your computer and use it in GitHub Desktop.
Save ericakfranz/d481791053c8eb3c9625 to your computer and use it in GitHub Desktop.
Custom action hook for custom conversion tracking in Canvas in OptinMonster.
jQuery(document).ready(function($){
$(document).on('click', '.om-custom-html-form a button', function(e){
// Prepare variables.
var $this = $(this),
optin = $this.closest('.optin-monster-overlay').attr('id').replace('om-', '').replace('-', '_');
if ( ! window[optin] ) {
return;
}
// Send ajax request to track the optin.
var data = {
optin_monster_ajax_action: 'track_optinmonster_custom',
id: window[optin].getProp('id')
};
$.post(window[optin].getProp('ajax'), data, function(res){}, 'json');
});
});
add_action( 'optin_monster_ajax_action', 'tgm_om_custom_tracker', 10, 2 );
function tgm_om_custom_tracker( $action, $data ) {
// If not our action, do nothing.
if ( 'track_optinmonster_custom' !== $action ) {
return;
}
// If our ID is not passed, do nothing.
if ( empty( $data['id'] ) ) {
return;
}
// Load our interfaces and track the conversion.
if ( ! class_exists( 'Optin_Monster_Track_Datastore' ) ) {
require plugin_dir_path( Optin_Monster::get_instance()->file ) . 'includes/global/track-datastore.php';
}
$track = new Optin_Monster_Track_Datastore( absint( $data['id'] ) );
$track->save( 'conversion' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment