Implementing 'Show Your Google Analytics Top Content in Wordpress'


After reading this article, I modified and implemented it in this blog. You can see it in the right column.

This is how I did it.


1. Download and install Exec-Php. Then activate it.
2. Go to ‘Widgets’. Select ‘Text’ from the Widgets and add the following code.
Please note that I do not want to show ‘ | Okada Design Blog’ or ‘ « Okada Design Blog’ in the title.
Also I don’t want to show some pages including the index page, ‘(not set)’, ‘Okada Design Blog’, ‘Downloads | Okada Design Blog’ and ‘Downloads « Okada Design Blog’

So you need to adjust it accordingly for your situations.

<?php
$start = date('Y-m-d', (time() - (60 * 60 * 24 * 30)));
$end = date('Y-m-d');
$showpages = 10;
$thispage = 1;

$login = new GADWidgetData();
$ga = new GALib($login->auth_token, $login->account_id, 60);
$pages = $ga->pages_for_date_period($start, $end);

echo "<ul>";
foreach($pages as $page) {

	$url = $page['value'];
	$title = $page['children']['value'];
$array = array('(not set)', 'Okada Design Blog', 'Downloads | Okada Design Blog', 'Downloads « Okada Design Blog');
// You need to add any words which you don't want to show in the list
if(!in_array($title, $array)){
$takeout = array(" | Okada Design Blog"," « Okada Design Blog");
// You need to add any words which you want to delete in the list
$newtitle = str_replace($takeout, "", $title);

	echo '<li><a href="' . $url . '" rel="nofollow">' . $newtitle . '</a></li>';
	$thispage++;
}

	if($thispage > $showpages) break;
}
echo "</ul>";
?>

3. Use SyntaxHighlighter Evolved for writing php code in your posts.
If you write php code with <?php and ?>, then the code will be executed in the post.
For example,

This is with php code.
This is the Exec-PHP ‘Hello World’. I am writing php code in the post directly. It starts with <?php and ends with ?>. End of php.

However if you use SyntaxHighlighter Evolved, it will not be executed.

<?php echo "This is with php code.<br />";
echo "This is the Exec-PHP 'Hello World'. I am writing php code in the post directly. It starts with &lt;?php and ends with ?&gt;. End of php.";  ?>

The credit goes to Rob Glazebrook and Alex.

4 comments to Implementing ‘Show Your Google Analytics Top Content in WordPress’

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>