Better way to track visitors on website.

Jon Imms
Jon Imms used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
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.
Fractional CTO
Distinguished Expert 2018
Commented:
You've hit a common problem, especially with Launch style traffic.

I've found WP Rocket handles this well. Likely best to purchase a copy + just use it to begin with.

Then in the future you can clone some of their code if you like.

WP Super Cache does attempt to do this + WP Super Cache has other problems.

At least last time I checked WP Super Cache correctly served cached content (from static HTML files) + then incorrectly every request would then bleed through to PHP. This means WP Super Cache fails with high traffic sites.

The https://wordpress.org/support/topic/wpsc-1-5-8-runs-mod_rewrite-then-passes-a-duplicate-request-to-php has been ignored by the developers... which is not a good sign...

Tip: There's also a completely different approach you might take to resolving this.

Depends on how you're logging your data. I'm just thinking... trying to conceptualize how your extending your query parameters...

The problem is always... involving PHP, so if you can avoid firing PHP, then your site can survive much easier.

It there's some way you can push this logic to run at the client/browser site + then make an async call back to Apache... to a static file (so no PHP involvement) with your appended query parameters, you'd have your data in the Apache log files (which you can extract easily) + no PHP drag.

Might not be doable for what you're trying to accomplish...

This is just one of the ways I'm exploring to fix a similar problem I'm having on many sites.
Jeffrey Dake Senior Director of Technologyy

Commented:
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).
Jon ImmsWeb Developer

Author

Commented:
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.
David FavorFractional CTO
Distinguished Expert 2018

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial