1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189:
<?php
class CTA_Conversion_Tracking {
public function __construct() {
self::load_hooks();
}
public static function load_hooks() {
add_action( 'inbound_track_link', array(__CLASS__, 'track_cta_link'));
add_filter('inboundnow_store_lead_pre_filter_data', array(__CLASS__, 'set_form_submission_conversion'), 20, 1 );
add_action( 'wp_cta_record_impression', array(__CLASS__, 'record_impression_event'), 10, 1);
add_action( 'wp_cta_record_impression', array(__CLASS__, 'update_impression_object'), 10, 1);
add_action( 'wp_cta_record_conversion', array(__CLASS__, 'record_conversion'), 10, 2);
}
public static function track_cta_link( $args ) {
$do_not_track = apply_filters('inbound_analytics_stop_track', false );
if ( $do_not_track || !isset($args['cta_id']) || !$args['cta_id'] || !wp_get_referer() ) {
return;
}
$event = array(
'event_name' => 'inbound_cta_click',
'cta_id' => (isset($args['cta_id'])) ? $args['cta_id'] : 0,
'page_id' => (isset($args['page_id'])) ? $args['page_id'] : 0,
'variation_id' => (isset($args['vid'])) ? $args['vid'] : 0
);
Inbound_events::store_event($event);
self::record_conversion( $args['cta_id'] , $args['vid'] );
}
public static function set_form_submission_conversion( $data ) {
parse_str($data['raw_params'], $raw_post_values );
if (!isset($raw_post_values['wp_cta_id']) || !$raw_post_values['wp_cta_id'] ) {
return $data;
}
$do_not_track = apply_filters('inbound_analytics_stop_track', false );
if ( $do_not_track ) {
return $data;
}
$cta_id = $raw_post_values['wp_cta_id'];
$vid = $raw_post_values['wp_cta_vid'];
$lp_conversions = get_post_meta( $cta_id, 'wp-cta-ab-variation-conversions-'.$vid, true );
$lp_conversions++;
update_post_meta($cta_id, 'wp-cta-ab-variation-conversions-'.$vid, $lp_conversions );
return $data;
}
public static function store_as_conversion( $args ) {
if (!isset($args['id']) || !$args['id'] ) {
return;
}
$time = current_time( 'timestamp', 0 );
$wordpress_date_time = date("Y-m-d G:i:s T", $time);
$conversion_data = get_post_meta( $args['id'] , 'wpleads_conversion_data', TRUE );
if (!$conversion_data) {
$conversion_data = array();
} else {
$conversion_data = json_decode($conversion_data,true);
}
$conversion_data[]['id'] = $args['cta_id'];
$conversion_data[]['variation'] = $args['vid'];
$conversion_data[]['datetime'] = $wordpress_date_time;
$conversion_data = json_encode($conversion_data);
update_post_meta( $args['id'], 'wpleads_conversion_data', $conversion_data );
update_post_meta( $args['id'], 'wpl-lead-conversion-count', count($conversion_data));
}
public static function record_impression_event( $event ) {
Inbound_Events::store_page_view(array(
'page_id' => (isset($event['page_id'])) ? $event['page_id'] : 0 ,
'cta_id' => (isset($event['cta_id'])) ? $event['cta_id'] : 0 ,
'variation_id' => (isset($event['variation_id'])) ? $event['variation_id'] : 0 ,
));
}
public static function update_impression_object( $event ) {
$impressions = get_post_meta( $event['cta_id'] ,'wp-cta-ab-variation-impressions-'.$event['variation_id'], true);
if (!is_numeric($impressions)) {
$impressions = 1;
} else {
$impressions++;
}
update_post_meta( $event['cta_id'], 'wp-cta-ab-variation-impressions-'.$event['variation_id'], $impressions);
}
public static function record_conversion( $cta_id, $vid ) {
$conversions = get_post_meta( $cta_id, 'wp-cta-ab-variation-conversions-' . $vid, true);
if (!is_numeric($conversions)) {
$conversions = 1;
} else {
$conversions++;
}
update_post_meta( $cta_id, 'wp-cta-ab-variation-conversions-'.$vid, $conversions);
}
}
new CTA_Conversion_Tracking();