簡體   English   中英

php標頭即時重定向(如果訪問了指定的廣告系列網址)

[英]php header instant redirect if specified campaign urls are visited

我可能在這里過分擔心,但是在這里...

我已經使用google campaign鏈接構建器設置了google分析活動跟蹤URL。

很棒,但是我喜歡20個不同的跟蹤網址,還有更多。

這些URL確實很丑陋,我不是訪問者的粉絲,因為該訪問者在第一次訪問我的網站時看到此長跟蹤URL。


這就是我要放在我的wordpress網站上的想法/理論。 如果有人願意幫助PHP編寫其中的一部分,我將不勝感激。 或任何建議,如果這個想法是一個壞主意。


例如,這些只是我的一些URL

http://example.com/?utm_source=Company&utm_medium=MPU&utm_campaign=促銷

http://example.com/?utm_source=Company&utm_medium=Leaderboard&utm_campaign=促銷

http://example.com/?utm_source=Company&utm_medium=Take%2BOver&utm_campaign=促銷

我也想立即重定向所有上述網址...

http://example.com/

在我的header.php或functions.php中使用php


有一些如何寫的,所以我可以簡單地在數組/案例中添加新的跟蹤URL。

任何建議將非常感謝!


請別笑了-但是我想這就是我想要做的...

$landing = $_SERVER['REQUEST_URI'];

$campaigns = array(
"http://example.com/?utm_source=Company&utm_medium=MPU&utm_campaign=Promo",
"http://example.com/?utm_source=Company&utm_medium=Leaderboard&utm_campaign=Promo",
"http://example.com/?utm_source=Company&utm_medium=Take%2BOver&utm_campaign=Promo"
);

if ( $campaigns == $landing ) {

header( 'Location: http://example.com/' ) ;

}

嘗試這個,

這是純JavaScript,可在加載頁面后從網址中清除utm_params。

function clear_utm_from_url(){
     var currentLocation = window.location.search.replace(/\?/g, '').split('&');
     var new_url = new Array();

     for( i = 0; i < currentLocation.length; i++){
         var q = currentLocation[i].split('=');
         if( q[0].search("utm_") ){
             new_url.push(currentLocation[i]);
         }
     }
     if( new_url.length > 0 ){
         new_url = "?"+new_url.join("&");   
     }else{
         new_url = "";
     }

     history.pushState({}, "", window.location.pathname+new_url);  
 }

並添加類似這樣的東西或其他可以運行的功能

<body onload="clear_utm_from_url();">

一種刪除查詢字符串的簡單方法:

if ($urlIsTracker) {
   list($url) = explode('?', $_SERVER['REQUEST_URI']);
   header("Location: $url", true, 301);
   exit;
}

你不應該那樣做。

Google Analytics(分析)跟蹤代碼是客戶端。 這是一段js代碼,頁面加載后立即執行。 如果找不到utm_ *查詢參數,則不會向ga發送任何內容,因此您將無法看到那里的統計信息。 很可能您會在ga ui中看到總計。

如果您仍然想這樣做...

$trackingParams = array('utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term');
$queryParams = array();
parse_str($_SERVER['QUERY_STRING'], $queryParams);
if (count(array_intersect($queryParams, $trackingParams)) > 1) {
    // This url has tracking params
    foreach ($trackingParams as $paramName) {
        unset($queryParams[$paramName]);
    }
    // You can use a regex, but i wouldn't recommend it
    $newUrl = str_replace(
        $_SERVER['QUERY_STRING'],
        http_build_query($queryParams),
        $_SERVER['REQUEST_URI']
    );
    header('Location: ' . $newUrl, true, 301);
    exit;
}

這是代碼將在當前URL中搜索utm_ *參數,並重定向到沒有它們的URL,而其他查詢參數保持不變。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM