TinyPortal

Development => Block Codes => Topic started by: Ianedres on April 25, 2008, 11:19:45 PM

Title: Google Chart For Total Hits
Post by: Ianedres on April 25, 2008, 11:19:45 PM
I kludged this up for a simple chart for my own purposes using TP & SMF since I'm eagerly awaiting the one million mark...
(https://www.tinyportal.net/proxy.php?request=http%3A%2F%2Fchart.apis.google.com%2Fchart%3Fcht%3Dbhs%26amp%3Bchs%3D160x40%26amp%3Bchd%3Dt%3A96.7756%257C100%26amp%3Bchco%3D0000ff%2C00ff00%26amp%3Bchf%3Dbg%2Cs%2C606060%26amp%3Bchbh%3D15%26amp%3Bchtt%3DTotal%2BHits%3A%2B967%2C756%26amp%3Bchts%3Dffffff&hash=02a35dea87705f53cc74423681d422f27e6a8902)

Basically, pulls the total hits of your site, calculates percentage against a target number, and call Google's Chart API to display a graphic representation with the total hits overlayed on the image. Google does all the graphics work dynamically using URL variables that the snippet ciphers. (Does not require an API key.)

Also added options to display for admins only and/or home page only; only runs the database query when necessary.

Can be customized for chart's alignment, height, width, bar colors, bar width, background color, title text color, etc.

Intended for a right or left column (i.e. narrow width) in a PHP block- don't forget to enable the block.

If you want everyone to view the chart, you will also need to set display options in the code itself, as it defaults to display to admins only on the home page only.

Added 07/31/08 : Must have daily page view tracking enabled through the admin panel.


/*
Chart total hits against target using Google Chart API
April 25, 2008
Tim Antley - www.bayoumx.com

For Use With SMF & TinyPortal

Uses code from SMF's source to retrieve total numbers of hits
Calculates percentage of hits against target number
Google Charts API plots image bases on above info

Option to display chart for admins only
Option to display chart on home page only or all pages.
Provided user variables for chart display customization
*/

// user variables
$admin_only = 'on'; // on = enable admins only to view chart
$home_only = 'on'; // on = display on home page only
$target = '1000000'; // target number
$width = '160'; // chart width
$height = '40'; // chart height
$align = 'center'; // chart alignment (left/right/center)
$bg = '606060'; // (rrggbb) background color
$bar1 = '0000ff'; // (rrggbb) bar color 1
$bar2 = '00ff00'; // (rrggbb) bar color 2
$t_color = 'ffffff'; // (rrggbb) title text color
$bar = '15'; // bar height
// end of variables

// link to Google's Chart API
$chart_url = 'http://chart.apis.google.com/chart';

// check to display on home page only or all pages
if(($home_only == 'on' && $context['current_action']=='') || $home_only != 'on')
{
// check for admin only setting
if(($admin_only == 'on' && $context['user']['is_admin']) || $admin_only != 'on')
{
// retrieve total number of hits from SMF database
global $txt, $scripturl, $db_prefix, $modSettings, $user_info, $context;

$result = db_query("
SELECT SUM(hits) AS hits
FROM {$db_prefix}log_activity", __FILE__, __LINE__);
$row = mysql_fetch_assoc($result);
$context['num_hits'] = $row['hits'];
$t_hits = $context['num_hits']; // $t_hits now equals total hits

mysql_free_result($result);

// calculate percentage & format title
$percentage = ($t_hits/$target)*100;
$chd = 't:'.$percentage.'|100';
$chtt = 'Total+Hits:+'.number_format($t_hits).'&chts='.$t_color; // use plus sign for space

// output div for chart - align using variable setting
echo '<div align="'.$align.'" id="hit_chart">';
echo '<img src="'.$chart_url.'?cht=bhs&chs='.$width.'x'.$height.'&chd='.$chd.'&chco='.$bar1.','.$bar2.'&chf=bg,s,'.
$bg.'&chbh='.$bar.'&chtt='.$chtt.'">';
echo '</div>';
}
}
Title: Re: Google Chart For Total Hits
Post by: FERNSIDEâ„¢ on May 11, 2008, 11:02:02 AM
Thanks for sharing mate :)
Title: Re: Google Chart For Total Hits
Post by: EdwinK on July 22, 2008, 10:04:09 PM
Thanks :)
Title: Re: Google Chart For Total Hits
Post by: Ianedres on July 22, 2008, 10:25:10 PM
You are most welcome. Enjoy!  8)
Title: Re: Google Chart For Total Hits
Post by: mrbean17 on July 30, 2008, 02:46:09 AM
Am I supposed to change something with the code? I'm showing a 0 for total hits.

Thanks
Title: Re: Google Chart For Total Hits
Post by: Ianedres on July 30, 2008, 06:36:48 AM
It works with SMF 1.1.5 and TP 0.9.8; are you using newer versions other than those?
Title: Re: Google Chart For Total Hits
Post by: mrbean17 on July 31, 2008, 01:09:32 AM
Using SMF 1.1.4 and TP 0.9.8
Title: Re: Google Chart For Total Hits
Post by: Ianedres on July 31, 2008, 07:18:05 AM
If you have this in a block under TP, it works for 1.1.4 and 1.1.5.

You must have it in a TP block- and not just sitting on the server, as it uses SMF database query functions to get the number of hits, which is then pushed to Google's charting API.

A link to your site would be good (via PM if you wish) to see whether Google is getting the number.
Title: Re: Google Chart For Total Hits
Post by: Ianedres on July 31, 2008, 05:05:55 PM
After viewing your website, it appears you do not have the 'Track daily page views' option enabled for your site.

Enable it through the Admin panel under the Features & Options / Basic Settings; you will find the option about half way through that section.

You have the daily stats enabled, as I can see some stats (new topics, new posts, new members, most online) but not the total count of page views.

Please try that and let me know if that worked.
Title: Re: Google Chart For Total Hits
Post by: AST3R1X on July 31, 2008, 05:16:22 PM
@ Ianedres

I had the same problem but you have solved that for me. Thankyou  :)
Title: Re: Google Chart For Total Hits
Post by: Ianedres on July 31, 2008, 06:43:31 PM
Always good to know that someone got some use out of it!  8)
Title: Re: Google Chart For Total Hits
Post by: Final60 on August 03, 2008, 01:08:22 AM
Thanks for this Ianedres