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

asked on

Set a time limit on Wordpress Cookie function.

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');

Open in new window

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.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jon Imms


Excellent. Thank you Sir.
Glad to help!