Hi SammyR,
I don’t also have much experience neither with modx nor php, but here is what I did.
I wanted to get the keyword and search engine where user comes from, and store that value in a cookie, and then use it to track clicks.
Below is some code, maybe it might help.
This one extract the keywords from major search engines. I found the extracting part from a website but can’t find the original address now.
So this puts the values in place holder.
<?php
$thereferrer = htmlspecialchars_decode($_SERVER['HTTP_REFERER']);
$url = parse_url( $thereferrer );
$possibles_params = array('q','qt','p');
$params = array();
parse_str($url['query'], $params);
foreach($possibles_params as $val)
{
if($params[$val]) {
$searchengine = $url['host'];
$keyword = $params[$val] ;
}
}
if ( isset($searchengine) && isset($keyword) ){
$modx->setPlaceholder('sessionresult', array( "searchengine"=>$searchengine,"keyword"=>$keyword ) );
}
else {
$modx->setPlaceholder('sessionresult', array( "searchengine"=>"other","keyword"=>"not found" ) );
}
There is another snippet which is called when a page is requested. "sp_SearchData_Get" is the previous snippet in this call. I tried to hold the values in a session, but it didn’t work. So keeping them in cookies meanwhile.
if ( !isset($_COOKIE["se"]) && !isset($_COOKIE["kw"]) ) {
$params=$modx->runSnippet("sp_SearchData_Get", $params);
$searchvalues= $modx->getPlaceholder('sessionresult');
setcookie("se", $searchvalues['searchengine'], time()+3600,'/',$_SERVER['HTTP_HOST']);
setcookie("kw", $searchvalues['keyword'], time()+3600,'/',$_SERVER['HTTP_HOST']);
}
You can get the values back with following code and use it for filling a hidden field.
$searchengine = $_COOKIE["se"];
$keyword = $_COOKIE["kw"];
hope it helps