FeedCount-Like Google Analytics Counter
FeedBurner's popular chicklet FeedCount is the most-used way to display the number of subscribers for a feed.
Also, a similar design is used by TwitterCounter to display your Twitter followers.
We'll be creating a dynamic FeedCount-like interface that can display your website's statistics like pageviews, visits, etc. (one or all of them) using PHP.

At the end of this tutorial, we'll have 2 different counters:
- a counter that can display only one metric – basic version
- a counter that can display multiple metrics (some jQuery fun here) – advanced version
Basic Counter
Let's start with the PHP part:
How To Fetch Google Analytics Data With PHP?
We'll be using a PHP class named GAPI that makes using Google Analytics API so easy.
There are 3 variables we need to start using GAPI:
GAPI offers a simple usage to access Google Analytics data:
Now we have the pageviews data of the last 30 days (it is possible to define custom dates, check the GAPI docs.)
(If you want to read more on GAPI, code-diesel has a great article on this class.)
Here is the HTML:
<div id="statsWrap"> <div id="statsDetailsWrap"> <div id="statsCount"><!--?php echo $ga--->getPageviews() ?></div> <div id="statsMetric">pageviews</div> </div> <div id="byGoogle">By Google Analytics</div> </div>
And CSS
#statsWrap {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
}
#statsDetailsWrap {
background: #333333;
float: left;
padding: 1px 3px 1px 1px;
border: #666666 1px inset;
}
#statsCount {
float: left;
padding: 1px 3px;
background: #999999;
color: #FFFFFF;
border: #666666 1px inset;
}
#statsMetric {
float: left;
padding: 3px;
color: #FFFFFF;
}
#byGoogle {
clear: both;
color: #666666;
font-size: 10px;
}
Advanced Counter (With jQuery)
It is possible to fetch multiple data with GAPI & we'll be creating an auto-rotating counter that can display multiple metrics (visits & pageviews).
Let's use a little improved version of the PHP code:
And a tiny update to the HTML by adding few spans to wrap every metric so we can show/hide them.
<div id="statsWrap"> <div id="statsDetailsWrap"> <div id="statsCount"><!--?php echo $ga---><span id="statsPageviews">getPageviews() ?></span> <!--?php echo $ga---><span id="statsVisits">getVisits() ?></span></div> <div id="statsMetric"><span id="statsPageviewsText">pageviews</span> <span id="statsVisitsText">visits</span></div> </div> <div id="byGoogle">By Google Analytics</div> </div>
I have also updated the CSS a little bit by adding hard-coded width-heights to elements to make sure they will look ok when rotating:
And here is the jQuery part which rotates metrics:
In the jQuery part, although it may look complicated, it is definitely not. We used callbacks for the slideUp/slideDown functions & added a little delay between transitions.
That's all.
- Google Analytics user that has access to the website stats to be displayed (I suggest creating a new Google Account specifically for this purpose and provide only read access to that website's data. This will prevent using your real Google account in your code which is a good step for security).
- Google Analytics user password.
- Profile ID of the website (it is not the one that starts with UA, rather, it is the ID that appears on browser's address bar when a report of that website is clicked).
<script type="text/javascript">
$(document).ready(function() {
$("#statsPageviews").hide();
$("#statsVisits").hide();
$("#statsPageviewsText").hide();
$("#statsVisitsText").hide();
rotateMetrics();
function rotateMetrics() {
$("#statsPageviewsText").slideDown("slow");
$("#statsPageviews").slideDown("slow", function() {
setTimeout(function() {
$("#statsPageviewsText").slideUp("slow");
$("#statsPageviews").slideUp("slow", function() {
$("#statsVisitsText").slideDown("slow");
$("#statsVisits").slideDown("slow", function() {
setTimeout(function() {
$("#statsVisitsText").slideUp("slow");
$("#statsVisits").slideUp("slow", function() {
rotateMetrics();
});
}, 3000);
});
});
}, 3000);
});
}
});
</script>
<style>
#statsWrap {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
}
#statsDetailsWrap {
background: #333333;
float: left;
padding: 1px 3px 1px 1px;
border: #666666 1px inset;
}
#statsCount {
float: left;
padding: 1px 3px;
background: #999999;
color: #FFFFFF;
border: #666666 1px inset;
width: 40px;
height: 12px;
}
#statsMetric {
float: left;
padding: 3px;
color: #FFFFFF;
width: 50px;
height: 12px;
}
#byGoogle {
clear: both;
color: #666666;
font-size: 10px;
}
</style>
</head>
<body>
<?php
define('ga_email','yourGoogleEmail');
define('ga_password','yourGooglePass');
define('ga_profile_id','yourProfileID');
require 'gapi.class.php';
$ga = new gapi(ga_email,ga_password);
$ga->requestReportData(ga_profile_id,array('browser','browserVersion'),array('pageviews','visits'));
?>
<div id="statsWrap">
<div id="statsDetailsWrap">
<div id="statsCount">
<span id="statsPageviews"><?php echo $ga->getPageviews() ?></span>
<span id="statsVisits"><?php echo $ga->getVisits() ?></span>
</div>
<div id="statsMetric">
<span id="statsPageviewsText">pageviews</span>
<span id="statsVisitsText">visits</span>
</div>
</div>
<div id="byGoogle">By Google Analytics</div>
</div>
- Tags:
Google Analytics Javascript Php
- Filed under: Extras, Goodies, No License, Statistics, Tutorials
- 21 Comments











21 Responses for "FeedCount-Like Google Analytics Counter"
[...] webresourcesdepot.com acaba de publicar un tutorial que nos puede ayudar a mostrar al mundo las visitas recibidas según Google Analytics. Con dos botones que recuerdan al estilo de Feedburner, podremos publicar las visitas únicas y las páginas vistas, todo con unos pequeños trucos en PHP que cualquiera familiarizado con la programación puede instalar fácilmente. [...]
Bang. Awesome tut.
Very nice, I will definitely use this.
What a great idea! It looks really cool…
But maybe you should consider caching the results of the API calls. Google Analytics data isn’t real-time anyways and during the time the Google API is taking to respond, your server won’t do anything (i.e. not serve). Worst case scenario is the Google API timing out and thus making your web site unusable. Plus Google will love you for not requesting the same data five thousand times per hour
[...] a FeedBurner and TwitterCounter like stats website statistics counter for Google Analytics Counter? WebResourcesDepot has introduced a tutorial for creating a dynamic metric counter based on the data of Google [...]
[...] Google Analytics Counter è uno script che permette di far visualizzare un contatore con gli accessi unici e pagine viste direttamente da Google Analitycs. [...]
sounds great . unfortunatly for very beginner users it doesn’t have a readme or something, i’m sure that is very easy to include this script in blog’s widget for those who have minimal knowledge.
if someone could write a few “step-by-step” instructions and what should we do with that css code.
i’ll prefer the “advanced version”.where should i put those 2 php files after i modify them with my analytics email account & pass , and also the js file where should be located ?
sorry if i annoy someone with my solicitations.thanks in advance.
Hi thanks for the tutorial and css of this counter badge. I use this css-based badge (or button?) for my Twitter Counter Follower Joomla module. I was looking for this badge for so long thanks finally you provide it. oh yes…this twitter follower counter Joomla module is released for free….so thanks again from the J! Community…
[...] Webresourcesdepot features a nifty little application using the above to dispay the Google pageviews in a Feed-burner chicklet style display. References: a. Google Analytics Data API – Data Feed b. Dimensions & Metrics Reference c. Filters Share this post [...]
Updated…Now this Google Analytics counter already converted as a Joomla module. thanks.
@Erwin,
That’s great job and thanks for linking back to the article.
i will use it thank u
thanks for sharing
[...] WebResourcesDepot ha pubblicato un tutorial in cui spiega come creare un contatore in stile Feedburner per mostrare quante visite abbiamo invece ricevuto con i dati di Google Analytics. [...]
Can I change the start date of the hit counter?
I would put the total visits (not monthly).
Thanks
I solved.
At line 191 of file gapi.class.php I replaced
$start_date = date (‘Ym-d’, strtotime (’1 month ago ‘));
with an example:
$start_date = date (’2009-10-22 ‘, strtotime (’1 month ago’));
@Kriesel,
Sure, check the start_date and end_date variables here: http://code.google.com/p/gapi-google-analytics-php-interface/wiki/GAPIDocumentation
help pls, i get this error >> http://img641.imageshack.us/img641/4986/23545435345345234563452.png
@plastik,
Although not sure, it seems like PHP is not working on your site for a reason. Or, there may be missing PHP tags (< ?php?>), check them too.
@plastik,
I get the same error, I add my code in my module, I allow php , but still this error..
where is the step buy step guide?
please.
this took a bit of modification but i finally got it working…. http://www.probeautyrewards.co.za right at the bottom
google profile id url has changed it looks like https://www.google.com/analytics/web/?hl=en&pli=1#report/visitors-overview/a00000000w11111111p22222222/
so to get your profile id number it’s the 8 numbers after the small p
thanks for the article