Link to home
Start Free TrialLog in
Avatar of Jon Imms
Jon ImmsFlag for United States of America

asked on

Better way to track visitors on website.

Hello Experts,

Our website currently tracks visitors to our website. Everybody who comes into the site with a UTM_source gets put in a cookie and tracks it across the site. IF somebody applies for a job, it will append the UTM string to the url, so the source gets credit. The code is below, and on our website

<?php
	
/*------------------------------------*\
   Add UTM Cookies
   -- This sets Tracking Cookies. 
/*------------------------------------*/
function set_ad_cookies() {
	$vars = array('utm_source', 'utm_campaign', 'utm_medium');
  //$parsed = array();
  	foreach ($vars as $k) {
    	if (isset($_GET[$k]) && !isset($_COOKIE['orig_'.$k])) {
        	setcookie('orig_'.$k, $_GET[$k], 0, '/', $domains['cookie_domain']);
    	}
   	}
  
   	if (!isset($_COOKIE['orig_utmsource'])) {
	 	setcookie('orig_utmsource', $_GET['utm_source'], 0, '/', $domains['cookie_domain']);
	}
}
add_action('init', 'set_ad_cookies');
/*------------------------------------*\
  Append UTMs to links.
  - IF you have form submissions, or similar,
  - This will append the UTM string to
  - the end of the URL, so you can see which
  - ADD the user came from. Add your URL below. 
/*------------------------------------*/
function replace_links($text) {
	$url = explode('?', 'https://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
	$addCookiesToLink = "";
	if (strlen($url[1]) > 1) {
		$text = str_replace('yourwebsiteurl', 'yourwebsiteurl?' . $url[1], $text);
	}
		else { //no parameters in URL. Cookies?
		if(isset($_COOKIE['orig_utmsource'])) {
			$addCookiesToLink .= "utm_source=" . $_COOKIE['orig_utmsource'];
		}
		elseif(isset($_COOKIE['orig_utm_source'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}
			$addCookiesToLink .= "utm_source=" . $_COOKIE['orig_utm_source'];
		}
		elseif(isset($_COOKIE['orig_utmmedium'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}	
			$addCookiesToLink .= "utm_medium=" . $_COOKIE['orig_utmmedium'];
		}
		elseif(isset($_COOKIE['orig_utmcampaign'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}	
			$addCookiesToLink .= "utm_campaign=" . $_COOKIE['orig_utmcampaign'];
		}
		elseif(isset($_COOKIE['orig_utm_campaign'])) {
			if(strlen($addCookiesToLink) > 0) {
				$addCookiesToLink .= "&";
			}	
			$addCookiesToLink .= "utm_campaign=" . $_COOKIE['orig_utm_campaign'];
		}
				if(strlen($addCookiesToLink) > 0) {
				//echo $addCookiesToLink;
					$text = str_replace('yourwebsiteurl', 'yourwebsiteurl?' . $addCookiesToLink, $text);
			
		}
		//echo "test1" . $_COOKIE['orig_utm_campaign'] . " 2:" . $_COOKIE['orig_utm_source'] . " 3:" . $_COOKIE['orig_utmcampaign'] . " 4:" . $_COOKIE['orig_utmmedium'] . " 5:" . $_COOKIE['orig_utmsource'];
	}
	return $text;
}
/* --------------------------------*\
	- SET Phone Number Cookies
	- If you have Dynamic Phone numbers
	- to track, then use this code.
/* ---------------------------------*/
function set_phone_cookie() {
	$url = explode('?', 'https://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
	$params = explode('&', $url[1]);
	$paramTotal = count($params);
	For ($i = 1; $i <= $paramTotal; $i++) {
		if (strpos($params[$i-1], 'utm_source') !== false) {
			$theParam = $params[$i-1];
		}
	}
	//find utm_source
	$utmExplode = explode('=', $theParam);
	//explode on "=" to get utm_source equaled
	$utmEquals = $utmExplode[1];
	if (strlen($utmEquals) > 1 || isset($_COOKIE['utm_source_phone'])) {
		if (!isset($_COOKIE['utm_source_phone'])) {
			setcookie('utm_source_phone', $utmEquals, 0, '/');
		}
		else {
			$utmEquals = $_COOKIE['utm_source_phone'];
		}
	}	
}

Open in new window


It's all working, but what is happening is that everybody coming in with a UTM_Source is coming in uncached, so out page speed and server load time goes up.  

What i'm trying to find/figure out,  Is their a way to track visitors like we do above, but without cookies, or allowing for the pages to be cached so it loads faster?
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Hi Jon Imms,

If you want to update your tracking strategy then it is a different story otherwise what you are trying to achieve is called Dynamic caching or Output caching.

I personally use WP Super Cache but I am sure there are others which you can use. If you are looking for server specific caching let me know and I can provide you more information i.e. how to configure it on Apache/IIS.

Regards,
Chinmay.
ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is there a reason you are not using google analytics to do that tracking for you?  If you put google anaytics on your site, it already integrates with UTM to track campaigns that you tie to your success goals (applying for a job).
Avatar of Jon Imms

ASKER

The application is on a 3rd party site.  

Driver sees an add on google, email, sms, external job board. comes to our site with a utm code. browses the site, then decides to apply for a job. They click apply, and then it takes them to a 3rd party site which houses all our applicants. So we need to pass that refer code to . the 3rd party app, so we know who to give credit to.

GA is great, but it does not tell us who applied for a job.
After reading your last update, seems like the simple way to do this is simply cookie visitors + associate data with the cookie.

If the person applies for a job, then they'll require setting up an account + inputing some information, so likely not even a cookie, just tracking user activity.

What you're talking about is...

1) Have third party (affiliate) pass a code, like ?affid=31 + then associate this with whatever counts as a conversion for you...

2) Track the conversion(s).

3) Payout compensation, based on whatever model you use.

I suppose you can attempt writing all this code (shudder)... Instead, start with ThriveCart or something similar, which builds in an affiliate management + payment system.