troubleshooting Question

Set a time limit on Wordpress Cookie function.

Avatar of Jon Imms
Jon ImmsFlag for United States of America asked on
3 Comments1 Solution216 ViewsLast Modified:
I have the following function on our site,  which captures UTM tracking codes in cookies.  Users click on external ads, or through external sites to get to our site, and the UTM is stored in the cookie.

function set_ad_cookies() {
  $vars = array('utm_source', 'utm_campaign', 'utm_media');
  //$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_'.$k])) {
          $parsed[$k] = $k.'='.urlencode($_COOKIE['orig_'.$k]);
      } elseif (isset($_GET[$k])) {
              // no need to handle get params here - they will be passed with $_SERVER['REQUEST_URI']
      #    $parsed[$k] = $k.'='.urlencode($_GET[$k]);
    if (!isset($_COOKIE['orig_utmsource'])) {
	  setcookie('orig_utmsource', $_GET['utm_source'], 0, '/', $domains['cookie_domain']);
	if (!isset($_COOKIE['orig_utmmedium'])) {
	  setcookie('orig_utmmedium', $_GET['utm_medium'], 0, '/', $domains['cookie_domain']);
	if (!isset($_COOKIE['orig_utmcampaign'])) {
	  setcookie('orig_utmcampaign', $_GET['utm_campaign'], 0, '/', $domains['cookie_domain']);

add_action('init', 'set_ad_cookies');

What I'm finding though, is the cookies are not expiring,  A user will come back to the site a few days, or weeks later, and still have the same UTM codes stored in a cookie.

Is it possible, and if so how,  can i modify this function, so that it kills the cookie after say 2 hours.  That way, if the user comes back to the site after 2 hours, through one of our ads, it has the new UTM codes stored in a new fresh cookie.
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros