我在Wordpress网站上使用PHP来执行此操作。如果您认为JavaScript或jQuery会更好,请告诉我。
我们的合作伙伴之一正在为我们运营Google Google Adword广告系列。我无法访问确切的广告系列网址,因此无法根据广告系列网址创建Cookie。业主希望跟踪用户在我们网站上提交表单时来自哪些广告系列。我的计划:
以下是我们的合作伙伴使用的Adword广告系列网址的示例:
UTM_keyword =字段%20service%20software_phrase&安培; UTM_source =付费%20Search&安培; GCLID = EAIaI
我要做的是在"?UTM_keyword ="之间抓取上述网址的部分内容。和"& UTM_source =" - 在这种情况下,它将是"字段%20service%20software_phrase"。
以下是我迄今为止开发的代码:
header.php - 每页<!DOCTYPE html>
之后
<?php
// load content-utm.php if the URI contains UTM_keyword
if (strpos($_SERVER['REQUEST_URI'], "UTM_keyword")){
// load content-utm.php
get_template_part( 'content', 'utm' );
}
?>
内容utm.php
<?php
$uriSegments = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
// strip out UTM_keyword=
$uriSegments2 = substr($uriSegments, 12);
// strip out everything after &UTM_source= including &UTM_source=
$uriSegments3 = substr($uriSegments2, 0, strpos($uriSegments2, "&UTM_source="));
$_SESSION['uriName'] = $uriSegments3;
// create a cookie
if (strpos($_SERVER['REQUEST_URI'], $uriSegments3)){
$cookie_name = $uriSegments3;
$cookie_value = "UTM";
// make it a session cookie that expires after the browser is closed
setcookie($cookie_name, $cookie_value, 0, "/");
}
// I know how to remove a cookie but I'm not sure how I would remove THIS type of cookie
?>
形状page.php文件
<?php
// if the cookie is set
if (isset($_COOKIE[$uriSegments3])){
// load content-deluxe-cookie-tracking.php
get_template_part( 'partners/content', 'deluxe-cookie-tracking' );
}
?>
内容豪华-cookie的tracking.php
<input type="hidden" id="1234567890" name="1234567890" value="<?php echo $uriSegments3 = $_SESSION['uriName']; ?>">
到目前为止:
form-page.php
中的语句返回true,
content-deluxe-cookie-tracking.php
加载,输入value="<?php echo $uriSegments3 = $_SESSION['uriName']; ?>"
是Cookie名称。form-page.php
中的语句返回false,
content-deluxe-cookie-tracking.php
无法加载 - 我已检查以确保Cookie存在。所以,例如:
www.mysitename.com/form-page/?UTM_keyword=field%20service%20software_phrase&UTM_source=Paid%20Search&gclid=EAIaI
有效,但是:
www.mysitename.com/form-page/
即使Cookie在其他页面上设置,也无法正常工作,例如:
www.mysitename.com/another-page/?UTM_keyword=field%20service%20software_phrase&UTM_source=Paid%20Search&gclid=EAIaI
答案 0 :(得分:0)
我对自己问题的最终解决方案是使用php $ _SESSION而不是cookie。通过这个解决方案,我可以创建一个会话:
的header.php
<?php
/**
* Header
*/
session_start();
?>
<!DOCTYPE html>
<?php
// if the URI contains UTM_keyword
if (strpos($_SERVER['REQUEST_URI'], "UTM_keyword")){
// load content-utm.php
get_template_part( 'content', 'utm' );
}
?>
内容utm.php
<?php
// parse the URI after the question mark (?)
$uriSegments = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
// strip out UTM_keyword=
$uriSegments2 = substr($uriSegments, 12);
// strip out everything after &UTM_source= including &UTM_source=
$uriSegments3 = substr($uriSegments2, 0, strpos($uriSegments2, "&UTM_source="));
// name the session I can reference later
$_SESSION['uriName'] = $uriSegments3;
?>
形状page.php文件
<?php
// if the session named uriName is set
if (isset(isset($_SESSION["uriName"])){
// load content-deluxe-cookie-tracking.php
get_template_part( 'partners/content', 'deluxe-cookie-tracking' );
}
?>
内容豪华-cookie的tracking.php
// echo the value of the session in the input
<input type="hidden" id="1234567890" name="1234567890" value="<?php echo $uriSegments3 = $_SESSION['uriName']; ?>">
提交表单和数据最终会在创建的CRM字段中检索此数据。