Last active
August 29, 2015 14:08
-
-
Save zaneclaes/46d8564ea30474daa3e5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// SET UP THESE VARIABLES; | |
$GOOGLE_ANALYTICS_ID = ''; | |
$IOS_STORE_URL = ''; | |
$PATH_TO_GOOGLE_ANALYTICS_FILE = '';// This is where the Google Analytics Helper Script is located | |
function loadGAN($id = null) { | |
global $gan, $PATH_TO_GOOGLE_ANALYTICS_FILE; | |
if($gan) { | |
return $gan; | |
} | |
require_once($PATH_TO_GOOGLE_ANALYTICS_FILE); | |
$gan = new GAN($id); | |
return $gan; | |
} | |
// | |
// Detect's the user's mobile client | |
// If it is a matched client, creates a referral item in parse and returns a redirect URL for the AppStore | |
// | |
function createReferral($params = array()) { | |
require_once 'Mobile_Detect.php'; | |
$detect = new Mobile_Detect; | |
global $GOOGLE_ANALYTICS_ID, $IOS_STORE_URL; | |
$clients = array('iPhone'=>$IOS_STORE_URL, | |
'iPod'=>$IOS_STORE_URL, | |
'iPad'=>$IOS_STORE_URL); | |
$device = null; | |
foreach($clients as $client => $url) { | |
if($detect->is($client)) { | |
$device = $client; | |
break; | |
} | |
} | |
$ganuid = isset($_COOKIE['ganuid']) ? $_COOKIE['ganuid'] : null; | |
if(!$device) { | |
loadGAN()->pageview(array('page'=>'appstore-desktop'), $params, $ganuid); | |
return $IOS_STORE_URL; | |
} | |
$v = $detect->version($device); | |
$params['ip'] = sha1($_SERVER['REMOTE_ADDR']); | |
$params['device'] = $device ? $device : 'OSX'; | |
$params['os_version'] = $v ? $v : ''; | |
$params['timestamp'] = time(); | |
$params['ganuid'] = $ganuid; | |
// TODO: | |
// 1. Store the $params as a new Referral object in a database | |
// 2. Extract a unique $objId from the referral object... | |
$gan = new GAN($GOOGLE_ANALYTICS_ID); | |
$gan->pageview(array('page'=>'appstore-mobile?ref='.$objId,'title'=>$objId), $params, $ganuid); | |
return $clients[$device]; | |
} | |
$from = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; | |
$from = explode('?', $from); | |
$from = sizeof($from) >= 2 ? $from[1] : ''; | |
$qps = explode('&', $from); | |
$params = array(); | |
foreach($qps as $qp) { | |
$parts = explode('=', $qp); | |
if(sizeof($parts)>=2 && strpos($parts[0], 'utm_')===0) { | |
$params[substr($parts[0],4)] = $parts[1]; | |
} | |
} | |
header('location: '. createReferral($params)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment