we are running a Magento 1.4.2.0 Webshop with google analytics. As we all know google attaches a querystring called the \"gclid-Param\" to the url.
The customer clicks t
After some more deep research inside the chaos of magento we found the solution to solve our Problem.
In the Url-Model of the Mage_Core exists a class rewrite.php. We created a custom model and overwrited the rewrite.php.
Inside of the function rewrite(), we added the following piece(marked as comments) of code:
//$url_params = false;
//if ($url_params = $_SERVER['QUERY_STRING'])
//$url_params = "?".$url_params;
if ($external === 'http:/' || $external === 'https:')
{
if ($isPermanentRedirectOption)
{
header('HTTP/1.1 301 Moved Permanently');
}
header("Location: ".$this->getTargetPath() //.$url_params);
exit;
}
else
{
$targetUrl = $request->getBaseUrl(). '/' . $this->getTargetPath();
}
$isRedirectOption = $this->hasOption('R');
if ($isRedirectOption || $isPermanentRedirectOption)
{
if (Mage::getStoreConfig('web/url/use_store') && $storeCode =
Mage::app()->getStore()->getCode())
{
$targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/'
.$this->getTargetPath();
}
if ($isPermanentRedirectOption)
{
header('HTTP/1.1 301 Moved Permanently');
}
header('Location: '.$targetUrl //.$url_params);
exit;
}
So i hope our solution helps others, who are facing the same problem.
Best regards Markus
In 1.9.1.0 this problem could be solved through patching in another class Mage_Core_Model_Url_Rewrite_Request/function _processRedirectOptions().
Just add after code
$targetUrl = $this->_request->getBaseUrl() . '/' . $this->_rewrite->getTargetPath();
$storeCode = $this->_app->getStore()->getCode();
if (Mage::getStoreConfig('web/url/use_store') && !empty($storeCode)) {
$targetUrl = $this->_request->getBaseUrl() . '/' . $storeCode . '/' . $this->_rewrite->getTargetPath();
}
if ($this->_rewrite->hasOption('R') || $isPermanentRedirectOption) {
the following
$queryString = $this->_getQueryString();
if ($queryString) {
$targetUrl .= '?'.$queryString;
}
and make sure 'if' statement keep closed with
$this->_sendRedirectHeaders($targetUrl, $isPermanentRedirectOption);
}
I'm sure it's fairly enough because of you don't need to transfer query string for external redirects.
Happy coding
I am running 2.1.0 community edition right now and am having the same issue. I tried finding the files above, but they seem to be specific to the 1.X versions of Magento (at least the implementation within the files). I've found a work around for this, but I hate hate hate the way I am doing it. That being said, I am not noticing any performance problems with the site and I didn't have to modify any Magento core files. So... here is what I did;
I already had a directory under the Magento root directory that I use to host static content.
I created two files in this directory: startscripts.js (which I use to load any custom scripts within the head element) and endscripts.js (which I use to load any custom scripts right before the end of the body element).
In the administration page, go to content, configuration, and edit your site.
In the html head section add
<script src="/[staticDirectoryYouCreate]/startscripts.js" type="text/javascript"></script>
to the scripts and stylesheets section
<script src="/[staticDirectoryYouCreate]/endscripts.js" type="text/javascript" defer></script>
to the Miscellaneous HTML section
Here is the script that I created in the endscripts.js file to append the gclid to links on the page:
try { var urlParameters = /gclid=([^&]+)/.exec(document.location.toString()); if(urlParameters) { if(urlParameters[1]) { var siteLinks = document.getElementsByTagName("a"); for(var currentLink=0;currentLink < siteLinks.length;currentLink++) { if(siteLinks[currentLink].href.toString().indexOf("gclid") < 0) { if(siteLinks[currentLink].href.toString().indexOf("?") < 0) { siteLinks[currentLink].href=siteLinks[currentLink].href+"?gclid="+urlParameters[1]; } else { siteLinks[currentLink].href=siteLinks[currentLink].href+"&gclid="+urlParameters[1]; } } } } } } catch(e){}
For this particular fix, you don't have to add the startscripts.js file, but the Google tag assistant was complaining that the Google Analytics snippet was outside of the head element, so I disabled the Magento Google Analytics implementation and put the snippet in the startscripts.js file.
I'll be curious to hear folks opinions on solving the problem this way.