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: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265:
<?php
class Inbound_Tracking {
public function __construct() {
self::load_hooks();
}
public static function load_hooks() {
add_filter( 'inbound_analytics_localized_data' , array( __CLASS__ , 'filter_lead_tracking_rules' ) );
add_filter( 'inbound_analytics_stop_track' , array( __CLASS__ , 'filter_conversions' ) );
add_action('admin_enqueue_scripts', array(__CLASS__, 'load_admin_scripts'));
add_filter( 'the_content' , array( __CLASS__ , 'prepare_tracked_links') , 110 , 1 );
add_filter( 'get_the_content' , array( __CLASS__ , 'prepare_tracked_links') , 110 , 1 );
add_filter( 'inbound_track_links' , array( __CLASS__ , 'prepare_tracked_links') , 110 , 1);
add_action( 'inbound_track_link', array(__CLASS__, 'track_link'));
add_action('wpleads_display_quick_stat', array(__CLASS__, 'display_quick_stat_content_clicks') , 15 );
}
public static function load_admin_scripts() {
global $post;
if (!isset($post) || strstr($post->post_type, 'inbound-')) {
return;
}
wp_register_script('inbound-track-link', INBOUND_PRO_URLPATH . 'assets/js/admin/track-link.js');
wp_enqueue_script('inbound-track-link');
}
public static function filter_lead_tracking_rules( $inbound_localized_data ) {
global $inbound_settings;
$disable = false;
$ignore_admin = ( isset( $inbound_settings['inbound-analytics-rules']['admin-tracking'] ) && $inbound_settings['inbound-analytics-rules']['admin-tracking'] == 'off' ) ? true : false;
if ( current_user_can( 'manage_options' ) && $ignore_admin ) {
$disable = true;
}
$ip_addresses = ( isset( $inbound_settings['inbound-analytics-rules']['ip-addresses']) ) ? $inbound_settings['inbound-analytics-rules']['ip-addresses'] : array();
if ( in_array( $inbound_localized_data['ip_address'] , $ip_addresses ) ) {
$disable = true;
}
if (!$disable) {
return $inbound_localized_data;
return $inbound_localized_data;
}
$inbound_localized_data['page_tracking'] = 'off';
$inbound_localized_data['search_tracking'] = 'off';
$inbound_localized_data['comment_tracking'] = 'off';
return $inbound_localized_data;
}
public static function filter_conversions( $do_not_track ) {
global $inbound_settings;
$ignore_admin = ( isset( $inbound_settings['inbound-analytics-rules']['admin-tracking'] ) && $inbound_settings['inbound-analytics-rules']['admin-tracking'] == 'off' ) ? true : false;
if ( current_user_can( 'manage_options' ) && $ignore_admin ) {
$do_not_track = true;
}
$ip_addresses = ( isset( $inbound_settings['inbound-analytics-rules']['ip-addresses']) ) ? $inbound_settings['inbound-analytics-rules']['ip-addresses'] : array();
if ( in_array( LeadStorage::lookup_ip_address() , $ip_addresses ) ) {
$do_not_track = true;
}
return $do_not_track;
}
public static function prepare_tracked_links( $html ) {
global $post;
if (
( !is_single() || !in_the_loop() || !is_main_query() )
&&
(current_filter() != 'inbound_track_links')
) {
return $html;
}
if (!strstr($html , 'inbound-track-link')) {
return $html;
}
$doc = new DOMDocument();
if (function_exists('libxml_use_internal_errors')) {
libxml_use_internal_errors(true);
$error = false;
}
if (!function_exists('mb_convert_encoding')) {
@$doc->loadHTML($html);
} else {
@$doc->loadHTML( mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
}
if (function_exists('libxml_use_internal_errors')) {
$error = libxml_get_last_error();
libxml_use_internal_errors(false);
}
if (isset($error->level) && $error->level == LIBXML_ERR_FATAL ) {
return $html;
}
foreach($doc->getElementsByTagName('a') as $anchor) {
$class = $anchor->getAttribute('class');
if (!strstr( $class, 'inbound-track-link' )) {
continue;
}
$href = $anchor->getAttribute('href');
$link = Inbound_API::analytics_track_links( array(
'page_id' => ( isset($post) && $post->ID ? $post->ID : null ) ,
'vid' => 0 ,
'url' => $href ,
'tracking_id' => __( 'Inbound Tracked Link', 'inbound-pro' )
));
$link['url'] = str_replace('&', '&' , $link['url'] );
$anchor->setAttribute('href', $link['url']);
}
$doc->saveHTML();
$html = '';
foreach($doc->getElementsByTagName('body')->item(0)->childNodes as $element) {
$html .= $doc->saveXML($element, LIBXML_NOEMPTYTAG);
}
$html = str_replace('<![CDATA[' , '' , $html);
$html = str_replace(']]>' , '' , $html);
return $html;
}
public static function track_link( $args ) {
$do_not_track = apply_filters('inbound_analytics_stop_track', false );
if ( $do_not_track || isset($args['cta_id']) || !$args['page_id'] || !wp_get_referer() ) {
return;
}
$args = array(
'event_name' => 'inbound_content_click',
'page_id' => $args['page_id'],
'variation_id' => (isset($args['vid'])) ? $args['vid'] : 0,
'event_details' => json_encode($args),
);
Inbound_Events::store_event($args);
}
public static function display_quick_stat_content_clicks() {
global $post;
$content_clicks = Inbound_Events::get_content_clicks( $post->ID )
?>
<div class="quick-stat-label">
<div class="label_1"><?php _e('Content Clicks', 'inbound-pro'); ?></div>
<div class="label_2">
<?php
if (class_exists('Inbound_Analytics')) {
?>
<a href='<?php echo admin_url('index.php?action=inbound_generate_report&lead_id='.$post->ID.'&class=Inbound_Event_Report&event_name=inbound_content_click&range=10000&title='. urlencode(Inbound_Events::get_event_label('inbound_content_click')).'&tb_hide_nav=true&TB_iframe=true&width=1000&height=600'); ?>' class='thickbox inbound-thickbox'>
<?php echo count($content_clicks); ?>
</a>
<?php
} else {
echo count($content_clicks);
}
?>
</div>
<div class="clearfix"></div>
</div>
<?php
}
}
new Inbound_Tracking();