Skip to content

Instantly share code, notes, and snippets.

@transbetty
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save transbetty/e23f4277c48416173f6c to your computer and use it in GitHub Desktop.
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
<?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