Integrating WordPress and Magento: A Fix for Google Analytics
In a previous post we discussed integrating your WordPress installation with your Magento header and footer. One problem we ran into with this approach was that Google Analytics was logging all page views on our blog as "empty.html". Obviously, this isn't ideal for tracking what content our readers are finding useful. It turns out the solution is an easy 4-step process.
You need to have already read through this post before moving on to the steps below. Remember to back up all your files before making any changes.
Step 1:
In your WordPress theme's functions.php file, replace this code:
public function __construct($url, $markerStartHeader = null, $markerEndHeader = null, $markerStartFooter = null, $markerEndFooter = null)
{
//create array for passing to Magento
$wp_variables = array();
//read WordPress title into variable array
$wp_variables['wp_title'] = wp_title('', false, 'right');
//initialize CURL handle
$ch = curl_init();
//set CURL URL
curl_setopt ($ch, CURLOPT_URL, $url);
//POST variables to the Magento page
curl_setopt($ch, CURLOPT_POST, 1);
//build a key/value string to send to Magento (Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.)
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($wp_variables));
//return the results as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//read results into _content
$this->_content = curl_exec($ch);
//close CURL handle
curl_close($ch);
$this->_renderHeader($markerStartHeader, $markerEndHeader);
$this->_renderFooter($markerStartFooter, $markerEndFooter);
}
With this:
public function __construct($url, $markerStartHeader = null, $markerEndHeader = null, $markerStartFooter = null, $markerEndFooter = null)
{
//create array for passing to Magento
$wp_variables = array();
//read WordPress title into variable array
$wp_variables['wp_title'] = wp_title('', false, 'right');
//send current REQUEST_URI to Magento
$wp_variables['request_uri'] = $_SERVER['REQUEST_URI'];
//initialize CURL handle
$ch = curl_init();
//set CURL URL
curl_setopt ($ch, CURLOPT_URL, $url);
//POST variables to the Magento page
curl_setopt($ch, CURLOPT_POST, 1);
//build a key/value string to send to Magento (Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.)
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($wp_variables));
//return the results as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//read results into _content
$this->_content = curl_exec($ch);
//close CURL handle
curl_close($ch);
$this->_renderHeader($markerStartHeader, $markerEndHeader);
$this->_renderFooter($markerStartFooter, $markerEndFooter);
}
We've added this code, which simply passes the current HTTP request URI to the Magento app:
//send current REQUEST_URI to Magento $wp_variables['request_uri'] = $_SERVER['REQUEST_URI'];
Step 2:
In your Magento installation, copy
/app/code/core/Mage/GoogleAnalytics/Block/Ga.php
to
/app/code/local/Mage/GoogleAnalytics/Block/Ga.php. (You will probably need to create these folders in the local directory).
Step 3:
In /app/code/local/Mage/GoogleAnalytics/Block/Ga.php, replace the following code around line 147:
public function getPageName()
{
if (!$this->hasData('page_name')) {
//$queryStr = '';
//if ($this->getRequest() && $this->getRequest()->getQuery()) {
// $queryStr = '?' . http_build_query($this->getRequest()->getQuery());
//}
$this->setPageName(Mage::getSingleton('core/url')->escape($_SERVER['REQUEST_URI']));
}
return $this->getData('page_name');
}
With this:
public function getPageName()
{
if (!$this->hasData('page_name')) {
//$queryStr = '';
//if ($this->getRequest() && $this->getRequest()->getQuery()) {
// $queryStr = '?' . http_build_query($this->getRequest()->getQuery());
//}
if ($_POST['request_uri']) {
$this->setPageName(Mage::getSingleton('core/url')->escape($_POST['request_uri']));
} else {
$this->setPageName(Mage::getSingleton('core/url')->escape($_SERVER['REQUEST_URI']));
}
}
return $this->getData('page_name');
}
Step 4:
Upload /app/code/local/Mage/GoogleAnalytics/Block/Ga.php and your WordPress theme's functions.php file to your server.
And we're done! You should now see Magento reporting the correct page to Google Analytics.



September 4th, 2010 - 18:10
To be honest, i havent tried integrating wordpress yet, but im fan of both so i bookmarked this one!