bux23
7/12/2016 - 2:50 PM

WP Create and GET Campaign variables and cookies

WP Create and GET Campaign variables and cookies

/* COOKIES AND SESS VARS */

function set_custom_cookies_and_vars() {
				
	// VARS
	$curdate = date("Y-m-d");
	$curSessID = session_id();
	$_SESSION['view_num'];
	$_SESSION['camp_name']; 
	$_SESSION['camp_source'];
	$_SESSION['camp_medium'];
	$_SESSION['camp_term'];
	$_SESSION['camp_content'];
	$_SESSION['first_view_ref'];
	$_SESSION['first_view'];
	$_SESSION['f_camp_name'];
	$_SESSION['f_camp_source'];
	$_SESSION['f_camp_medium'];
	$_SESSION['f_camp_term'];
	$_SESSION['f_camp_content'];
	$_SESSION['f_camp_ref'];
	
	if (!isset($_SESSION['refer_url']))	{
		$_SESSION['refer_url'] = $_SERVER['HTTP_REFERER'];	
	}
	
	// GET VARS
	if (isset($_GET['utm_campaign'])) {
		$_SESSION['camp_name'] = $_GET['utm_campaign'];
	} else {
		$_SESSION['camp_name'] = "null";
	}
	if (isset($_GET['utm_source'])) {
		$_SESSION['camp_source'] = $_GET['utm_source'];
	} else {
		$_SESSION['camp_source'] = "null";
	}
	if (isset($_GET['utm_medium'])) {
		$_SESSION['camp_medium'] = $_GET['utm_medium'];
	} else {
		$_SESSION['camp_medium'] = "null";
	}
	if (isset($_GET['utm_term'])) {
		$_SESSION['camp_term'] = $_GET['utm_term'];
	} else {
		$_SESSION['camp_term'] = "null";
	}
	if (isset($_GET['utm_content'])) {
		$_SESSION['camp_content'] = $_GET['utm_content'];
	} else {
		$_SESSION['camp_content'] = "null";
	}
	if(isset($_GET['utm_campaign']) || isset($_GET['utm_source']) || isset($_GET['utm_medium']) || isset($_GET['utm_term']) || isset($_GET['utm_content'])) {
		$_SESSION['camp_ref'] = $_SERVER['HTTP_REFERER'];
	} else {
		$_SESSION['camp_ref'] = "null";
	}

	
	//COOKIES
	if (!isset($_COOKIE['sessionID']) || $_COOKIE['sessionID'] != $curSessID) {
		setcookie("sessionID", $curSessID, time() + (10 * 365 * 24 * 60 * 60), "/", COOKIE_DOMAIN, false);
	}

	if (!isset($_COOKIE['first_view'])) {	
		setcookie("first_view", $curdate, time() + (10 * 365 * 24 * 60 * 60), "/", COOKIE_DOMAIN, false);
		setcookie("first_view_ref", $_SESSION['refer_url'], time() + (10 * 365 * 24 * 60 * 60), "/", COOKIE_DOMAIN, false);
	}
	
	if (isset($_COOKIE['view_num']) && ($_COOKIE['sessionID'] != $curSessID)) {
		$_SESSION['view_num'] = $_COOKIE['view_num'];
		$_SESSION['view_num']++;
		setcookie("view_num",$_SESSION['view_num'], time() + (10 * 365 * 24 * 60 * 60), "/", COOKIE_DOMAIN, false);
	} else if (!isset($_COOKIE['view_num'])) {
		$_SESSION['view_num'] = 1;
		setcookie("view_num",$_SESSION['view_num'], time() + (10 * 365 * 24 * 60 * 60), "/", COOKIE_DOMAIN, false);
	} else if (isset($_COOKIE['view_num'])) {
		$_SESSION['view_num'] = $_COOKIE['view_num'];
	}
	
	
	if ($_SESSION['camp_name'] != "null" || $_SESSION['camp_source'] != "null" || $_SESSION['camp_medium'] != "null" || $_SESSION['camp_term'] != "null" || $_SESSION['camp_content'] != "null") {
		if (!isset($_COOKIE['camp_name']) && !isset($_COOKIE['camp_source']) && !isset($_COOKIE['camp_medium']) && !isset($_COOKIE['camp_term']) && !isset($_COOKIE['camp_content']) && !isset($_COOKIE['f_camp_ref']) ) {
			setcookie("camp_name", $_SESSION['camp_name'], time() + (60 * 60 * 24 * 30), "/", COOKIE_DOMAIN, false);
			setcookie("camp_source", $_SESSION['camp_source'], time() + (60 * 60 * 24 * 30), "/", COOKIE_DOMAIN, false);
			setcookie("camp_medium", $_SESSION['camp_medium'], time() + (60 * 60 * 24 * 30), "/", COOKIE_DOMAIN, false);
			setcookie("camp_term", $_SESSION['camp_term'], time() + (60 * 60 * 24 * 30), "/", COOKIE_DOMAIN, false);
			setcookie("camp_content", $_SESSION['camp_content'], time() + (60 * 60 * 24 * 30), "/", COOKIE_DOMAIN, false);
			setcookie("first_camp_ref", $_SESSION['refer_url'], time() + (60 * 60 * 24 * 30), "/", COOKIE_DOMAIN, false);
		}
	}
	
	if(isset($_COOKIE['first_view_ref'])) {
		$_SESSION['first_view_ref'] = $_COOKIE['first_view_ref'];
	} else {
		$_SESSION['first_view_ref'] = $_SESSION['refurl'];
	}
	
	if(isset($_COOKIE['first_view'])) {
		$_SESSION['first_view'] = $_COOKIE['first_view'];
	} else {
		$_SESSION['first_view'] = $curdate;
	}
	
	if(isset($_COOKIE['camp_name'])) {
		$_SESSION['f_camp_name'] = $_COOKIE['camp_name'];
	} else {
		$_SESSION['f_camp_name'] = $_SESSION['camp_name'];
	}
	
	if(isset($_COOKIE['camp_source'])) {
		$_SESSION['f_camp_source'] = $_COOKIE['camp_source'];
	} else {
		$_SESSION['f_camp_source'] = $_SESSION['camp_source'];
	}
	
	if(isset($_COOKIE['camp_medium'])) {
		$_SESSION['f_camp_medium'] = $_COOKIE['camp_medium'];
	} else {
		$_SESSION['f_camp_medium'] = $_SESSION['camp_medium'];
	}
	
	if(isset($_COOKIE['camp_term'])) {
		$_SESSION['f_camp_term'] = $_COOKIE['camp_term'];
	} else {
		$_SESSION['f_camp_term'] = $_SESSION['camp_term'];
	}
	
	if(isset($_COOKIE['camp_content'])) {
		$_SESSION['f_camp_content'] = $_COOKIE['camp_content'];
	} else {
		$_SESSION['f_camp_content'] = $_SESSION['camp_content'];
	}
	
	if(isset($_COOKIE['f_camp_ref'])) {
		$_SESSION['f_camp_ref'] = $_COOKIE['f_camp_ref'];
	} else {
		$_SESSION['f_camp_ref'] = $_SESSION['camp_ref'];
	}
}
add_action( 'init', 'set_custom_cookies_and_vars');