Skip to content
This repository has been archived by the owner on Nov 17, 2020. It is now read-only.

Commit

Permalink
add unit tests, fixes, more readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Merrick committed Sep 5, 2012
1 parent d8adf4f commit 8a61d31
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 13 deletions.
15 changes: 14 additions & 1 deletion README.md
@@ -1,8 +1,21 @@
referral-grabber-php
====================

A PHP library for reading a website visitor's referral source information (e.g. what ad campaign, keywords a user clicked to come to my website) from their Google Analytics cookie.
A PHP library for reading a website visitor's referral source information (e.g. what ad campaign, keywords a user clicked to come to my website) from their Google Analytics cookie. Useful for saving this information when, for example, a new user signs up on your website, to later analyze your customer acquisition channels.

Usage
-----

The [Google Analytics](http://analytics.google.com) tracking script must be installed on the site making the request. See the [documentation](http://support.google.com/analytics/bin/answer.py?hl=en&answer=1008015&topic=1727146&ctx=topic) for more information on the proper installation of Google Analytics.

Include ```ReferralGrabber.php``` in your PHP code, and then call ```$data = ReferralGrabber::parseGoogleCookie($_COOKIE['__utmz']);```

The returned ```$data``` array will contain a map of the keys (source, medium, term, content, campaign, gclid) and their respective values.

Recommendations
---------------

Save all of these referrer parameters in your users table!

Acknowlegements
-----------------
Expand Down
24 changes: 12 additions & 12 deletions src/ReferralGrabber.php
Expand Up @@ -23,12 +23,12 @@ public static function strExtractBetween($haystack, $start, $end) {

public static function parseGoogleCookie($cookieString) {
$data = array();
$data['source'] = AnalyticsCookieParser::strExtractBetween($cookieString, 'utmcsr=', '|');
$data['medium'] = AnalyticsCookieParser::strExtractBetween($cookieString, 'utmcmd=', '|');
$data['term'] = AnalyticsCookieParser::strExtractBetween($cookieString, 'utmctr=', '|');
$data['content'] = AnalyticsCookieParser::strExtractBetween($cookieString, 'utmcct=', '|');
$data['campaign'] = AnalyticsCookieParser::strExtractBetween($cookieString, 'utmccn=', '|');
$data['gclid'] = AnalyticsCookieParser::strExtractBetween($cookieString, 'utmgclid=', '|');
$data['source'] = ReferralGrabber::strExtractBetween($cookieString, 'utmcsr=', '|');
$data['medium'] = ReferralGrabber::strExtractBetween($cookieString, 'utmcmd=', '|');
$data['term'] = ReferralGrabber::strExtractBetween($cookieString, 'utmctr=', '|');
$data['content'] = ReferralGrabber::strExtractBetween($cookieString, 'utmcct=', '|');
$data['campaign'] = ReferralGrabber::strExtractBetween($cookieString, 'utmccn=', '|');
$data['gclid'] = ReferralGrabber::strExtractBetween($cookieString, 'utmgclid=', '|');

//special provision: gclid will be set alone, but always indicates google cpc
if($data['gclid'] != '-') {
Expand All @@ -40,12 +40,12 @@ public static function parseGoogleCookie($cookieString) {

public static function parseHubspotCookie($cookieString) {
$data = array();
$data['source'] = AnalyticsCookieParser::strExtractBetween($cookieString, 'ptmcsr=', '|');
$data['medium'] = AnalyticsCookieParser::strExtractBetween($cookieString, 'ptmcmd=', '|');
$data['term'] = AnalyticsCookieParser::strExtractBetween($cookieString, 'ptmctr=', '|');
$data['content'] = AnalyticsCookieParser::strExtractBetween($cookieString, 'ptmcct=', '|');
$data['campaign'] = AnalyticsCookieParser::strExtractBetween($cookieString, 'ptmccn=', '|');
$data['gclid'] = AnalyticsCookieParser::strExtractBetween($cookieString, 'ptmgclid=', '|');
$data['source'] = ReferralGrabber::strExtractBetween($cookieString, 'ptmcsr=', '|');
$data['medium'] = ReferralGrabber::strExtractBetween($cookieString, 'ptmcmd=', '|');
$data['term'] = ReferralGrabber::strExtractBetween($cookieString, 'ptmctr=', '|');
$data['content'] = ReferralGrabber::strExtractBetween($cookieString, 'ptmcct=', '|');
$data['campaign'] = ReferralGrabber::strExtractBetween($cookieString, 'ptmccn=', '|');
$data['gclid'] = ReferralGrabber::strExtractBetween($cookieString, 'ptmgclid=', '|');

//special provision: gclid will be set alone, but always indicates google cpc
if($data['gclid'] != '-') {
Expand Down
63 changes: 63 additions & 0 deletions test/ReferralGrabberTest.php
@@ -0,0 +1,63 @@
<?php

set_include_path(get_include_path() . PATH_SEPARATOR . dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'src');
require_once 'ReferralGrabber.php';

/**
* Simple array compare
*/
function assertEquals($expected, $actual, $message = '') {
if( $expected != $actual ) {
$message ? print $message . "\n" : true ;
print "FAILED asserting that ".json_encode($expected)." is equal to ".json_encode($actual)."\n";
}
}

print "Running test suite\n";

$data = array('source' => '-',
'medium' => '-',
'term' => '-',
'content' => '-',
'campaign' => '-',
'gclid' => '-');

assertEquals($data, ReferralGrabber::parseGoogleCookie(''), 'Empty string');

$data = array('source' => '-',
'medium' => '(none)',
'term' => '-',
'content' => '-',
'campaign' => '-',
'gclid' => '-');

assertEquals($data, ReferralGrabber::parseGoogleCookie('utmcmd=(none)'), 'Single parameter');

$data = array('source' => 'google',
'medium' => '(none)',
'term' => '-',
'content' => '-',
'campaign' => 'camp1',
'gclid' => '-');

assertEquals($data, ReferralGrabber::parseGoogleCookie('utmcsr=google|utmccn=camp1|utmcmd=(none)'), 'Multiple parameters');

$data = array('source' => 'google',
'medium' => 'cpc',
'term' => '-',
'content' => '-',
'campaign' => '-',
'gclid' => '1234567');

assertEquals($data, ReferralGrabber::parseGoogleCookie('utmgclid=1234567'), 'Basic GCLID');

$data = array('source' => 'google',
'medium' => '(none)',
'term' => '-',
'content' => '-',
'campaign' => 'camp1',
'gclid' => '-');

assertEquals($data, ReferralGrabber::parseGoogleCookie('10000000000.0000.1utmcsr=google|utmccn=camp1|utmother2=parameter2|utmcmd=(none)|utmother1=parameter1'), 'Ignore extra junk');

print "Finished running test suite\n";

0 comments on commit 8a61d31

Please sign in to comment.