Last active
August 29, 2015 14:16
-
-
Save transbetty/e23f4277c48416173f6c to your computer and use it in GitHub Desktop.
A small PHP Script to add usual Google Analytics utm_ variables to outgoing links so you can track your referrers in NATS system. Warning: don't use sever-side page cache with this script
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 | |
/* | |
A small PHP Script to add usual Google Analytics utm_ variables to outgoing links so you can track your referrers in NATS system. This works only if NATS has Automatic Campaigns (autocampaigns) enabled: http://wiki.toomuchmedia.com/index.php/Automatic_Campaigns tags | |
NATS 4.1 also supports tags / autotags which can be handy to split utm_ parameters apart | |
Sample outgoing affil link http://www.affiliate.com/?utm_source=TS&utm_campaign=Header | |
author: transbetty@gmail.com, www.transbetty.com | |
WARNING: don't use sever-side page cache with this script - obviously it will cause the first page visit to generate some referral the code, unless you create an exception in your cache (little bit complicated). Use a JS/cookie solution instead: https://gist.github.com/transbetty/ffa7374d8f71667a1e44 | |
*/ | |
// if GA utm query var exists, put it into cookie, function setter | |
function setTrackSource () { | |
$my_utm_source = $_GET['utm_source']; | |
$my_utm_campaign = $_GET['utm_campaign']; | |
if ((!empty($my_utm_source) || !empty($my_utm_campaign)) && !(isset($_COOKIE['trackSource'])) ) { | |
$autocampValue = $my_utm_source . ",". $my_utm_campaign; | |
setcookie('trackSource', $autocampValue, time()+3600*24*30, '/'); | |
//echo "yes"; | |
} | |
} | |
//output GA utm if present in query var or cookie, function getter | |
function getTrackSource () { | |
$my_utm_source = $_GET['utm_source']; | |
$my_utm_campaign = $_GET['utm_campaign']; | |
if (!empty($my_utm_source) || !empty($my_utm_campaign)) { | |
$result = "/?autocamp=".$my_utm_source . ",". $my_utm_campaign; | |
} elseif (isset($_COOKIE['trackSource']) ) { | |
$result = "/?autocamp=". $_COOKIE['trackSource']; | |
} | |
echo $result; | |
} | |
setTrackSource (); | |
getTrackSource(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment