Created
July 13, 2011 03:59
-
-
Save supercleanse/1079694 to your computer and use it in GitHub Desktop.
Pretty Link Tracking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This is where the magic happens! | |
function track_link($slug,$values) | |
{ | |
global $wpdb, $prli_click, $prli_options, $prli_link, $prli_update; | |
$query = "SELECT * FROM ".$prli_link->table_name." WHERE slug='$slug' LIMIT 1"; | |
$pretty_link = $wpdb->get_row($query); | |
$pretty_link_target = apply_filters('prli_target_url',array('url' => $pretty_link->url, 'link_id' => $pretty_link->id)); | |
$pretty_link_url = $pretty_link_target['url']; | |
if(isset($pretty_link->track_me) and $pretty_link->track_me) | |
{ | |
$first_click = 0; | |
$click_ip = isset($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:''; | |
$click_referer = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:''; | |
$click_uri = isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:''; | |
$click_user_agent = isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:''; | |
//Set Cookie if it doesn't exist | |
$cookie_name = 'prli_click_' . $pretty_link->id; | |
//Used for unique click tracking | |
$cookie_expire_time = time()+60*60*24*30; // Expire in 30 days | |
if(!isset($_COOKIE[$cookie_name])) | |
{ | |
setcookie($cookie_name,$slug,$cookie_expire_time,'/'); | |
$first_click = 1; | |
} | |
if(isset($prli_options->extended_tracking) and $prli_options->extended_tracking == 'extended') | |
{ | |
$click_browser = $this->php_get_browser(); | |
$click_host = gethostbyaddr($click_ip); | |
$visitor_cookie = 'prli_visitor'; | |
//Used for visitor activity | |
$visitor_cookie_expire_time = time()+60*60*24*365; // Expire in 1 year | |
// Retrieve / Generate visitor id | |
if(!isset($_COOKIE[$visitor_cookie])) | |
{ | |
$visitor_uid = $prli_click->generateUniqueVisitorId(); | |
setcookie($visitor_cookie,$visitor_uid,$visitor_cookie_expire_time,'/'); | |
} | |
else | |
$visitor_uid = $_COOKIE[$visitor_cookie]; | |
} | |
else | |
{ | |
$click_browser = array( 'browser' => '', 'version' => '', 'platform' => '', 'crawler' => '' ); | |
$click_host = ''; | |
$visitor_uid = ''; | |
} | |
if($prli_options->extended_tracking != 'count') | |
{ | |
//Record Click in DB | |
$insert_str = "INSERT INTO {$prli_click->table_name} (link_id,vuid,ip,browser,btype,bversion,os,referer,uri,host,first_click,robot,created_at) VALUES (%d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%d,%d,NOW())"; | |
$insert = $wpdb->prepare($insert_str, $pretty_link->id, | |
$visitor_uid, | |
$click_ip, | |
$click_user_agent, | |
$click_browser['browser'], | |
$click_browser['version'], | |
$click_browser['platform'], | |
$click_referer, | |
$click_uri, | |
$click_host, | |
$first_click, | |
$this->this_is_a_robot($click_user_agent,$click_browser)); | |
$results = $wpdb->query( $insert ); | |
do_action('prli_record_click',array('link_id' => $pretty_link->id, 'click_id' => $wpdb->insert_id, 'url' => $pretty_link_url)); | |
} | |
else | |
{ | |
global $prli_link_meta; | |
$exclude_ips = explode(",", $prli_options->prli_exclude_ips); | |
if(!in_array($click_ip, $exclude_ips) and !$this->this_is_a_robot($click_user_agent,$click_browser)) | |
{ | |
$clicks = $prli_link_meta->get_link_meta($pretty_link->id, 'static-clicks', true); | |
$clicks = (empty($clicks) or $clicks === false)?0:$clicks; | |
$prli_link_meta->update_link_meta($pretty_link->id, 'static-clicks', $clicks+1); | |
if($first_click) | |
{ | |
$uniques = $prli_link_meta->get_link_meta($pretty_link->id, 'static-uniques', true); | |
$uniques = (empty($uniques) or $uniques === false)?0:$uniques; | |
$prli_link_meta->update_link_meta($pretty_link->id, 'static-uniques', $uniques+1); | |
} | |
} | |
} | |
} | |
// Reformat Parameters | |
$param_string = ''; | |
if(isset($pretty_link->param_forwarding) and ($pretty_link->param_forwarding == 'custom' OR $pretty_link->param_forwarding == 'on') and isset($values) and count($values) >= 1) | |
{ | |
$first_param = true; | |
foreach($values as $key => $value) | |
{ | |
if($first_param) | |
{ | |
$param_string = (preg_match("#\?#", $pretty_link_url)?"&":"?"); | |
$first_param = false; | |
} | |
else | |
$param_string .= "&"; | |
$param_string .= "$key=$value"; | |
} | |
} | |
if(isset($pretty_link->nofollow) and $pretty_link->nofollow) | |
header("X-Robots-Tag: noindex, nofollow", true); | |
switch($pretty_link->redirect_type) | |
{ | |
case '301': | |
header("HTTP/1.1 301 Moved Permanently"); | |
header('Location: '.$pretty_link_url.$param_string); | |
break; | |
default: | |
if( $pretty_link->redirect_type == '307' or | |
!$prli_update->pro_is_installed_and_authorized() ) | |
{ | |
if($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0') | |
header("HTTP/1.1 302 Found"); | |
else | |
header("HTTP/1.1 307 Temporary Redirect"); | |
header('Location: '.$pretty_link_url.$param_string); | |
} | |
else | |
do_action('prli_issue_cloaked_redirect', $pretty_link->redirect_type, $pretty_link, $pretty_link_url, $param_string); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment