Forums » Developers



Sample code for custom email reports!

Hi,

I have seen a lot of posts about customizing emails. Here's some great code you can do it with. It even has short links for every entry, so you can hop right to a certain page! Please post if you need clarification.

I will post an example output in the next post.

XML parsing is built into PHP and so this is nice and easy.

Sorry the indents are going to drop out of this post... but I hope it helps.

1. Sub your clicky site id and key

2. customize your titles

3. sub "days" for number of days history e.g. 7, 30

4. Fix the HTML - i had to modify it to get it to post!



function handle_clicky_xml( $days )
{
$map = array(
'visitors' => "Number of Visitors, $days days",
'visitors-unique' => "Number of Unique Visitors, $days Days",
'visitors-most-active' => 'Your Most Active Visitors',
'bounce-rate' => 'Bounce Rate',

'pages' => 'Top Pages',
'web-browsers' => 'Top Web Browsers',
'countries' => 'Top Countries',
'cities' => 'Top Cities',
'regions' => 'Top Regions',
);

$subtitle = array(
'visitors' => '',
'visitors-unique' => '',
'bounce-rate' => '',

'pages' => 'Your most popular pages.[br>[br>',
'visitors-most-active' => 'Here is how to put a title for the section.',
'web-browsers' => '',
'countries' => '',
'cities' => '',
'regions' => '',
);

$cmd = "curl -s "http://stats.sellebrity.com/api/stats/4?site_id=$this->clicky_site_id&sitekey=$this->clicky_admin&type=visitors,visitors-unique,bounce-rate,visitors-most-active,pages,countries,cities,regions,web-browsers&date=previous-$days-days" > /tmp/junk";
$out = `$cmd`;
$raw_xml = file_get_contents( "/tmp/junk" );
$str = '';
$xml = new SimpleXMLElement($raw_xml);

## any visitors? if no, don't bother making report

foreach ( $xml as $thing )
{
$type = strval($thing->attributes());

## abort if less than 10 visitors (don't send blank emails)
$value = $thing->date->item[0]->value;
if ( $type == 'visitors' && $value [ 10 )
return "";

$str .= "[h3]".$map[$type].":[/h3]n";
$str .= "[div]".$subtitle[$type]."[/div]n";
$str .= "[table class=mytable]";
for ( $i=0; $i[sizeof( $thing->date->item ); $i++ )
{
$title = $thing->date->item[$i]->title;
$value = $thing->date->item[$i]->value;
$value_percent = $thing->date->item[$i]->value_percent;
$url = $thing->date->item[$i]->url;
$stats_url = $thing->date->item[$i]->stats_url;

if ( $value_percent )
$str .= "[tr][td class=mycell valign=top]$value[/td][td class=mycell]($value_percent%)[/td][td class=mycell][a target=new href=$url]$title[/a][/td][td class=mycell nowrap][a target=new class=shaded href=$stats_url]see details[/a][/td][/tr]n";
elseif ( $stats_url )
$str .= "[tr][td class=mycell valign=top]$value[/td][td class=mycell][/td][td class=mycell][a target=new href=$url]$title[/a][/td][td class=mycell nowrap>[a target=new class=shaded href=$stats_url]see details[/a][/td][/tr]n";
else
$str .= "[tr][td class=mycell valign=top]$value[/td][td class=mycell][/td][td class=mycell][a target=new href=$url]$title[/a][/td][td class=mycell nowrap][/td][/tr]n";
}
$str .= "[/table]";
}
return $str;
}

Posted Tue Jan 25 2011 11:11p by serenex***


Sample output:


Number of Visitors, 7 days:

13664

Number of Unique Visitors, 7 Days:

12936

Bounce Rate:

89.3

Your Most Active Visitors:

52 (0.4%) 173.20.251.240 see details
13 (0.1%) 150.70.66.185 see details
11 (0.1%) 71.23.148.167 see details
9 (0.1%) 97.86.230.117 see details
9 (0.1%) 150.70.64.194 see details
8 (0.1%) 24.141.131.226 see details
7 (0.1%) 76.219.180.142 see details
7 (0.1%) 67.234.105.144 see details
7 (0.1%) 71.172.84.141 see details
6 178.130.4.21 see details

Top Pages:

Your most popular pages.

984 (6.2%) Name of page title (see details)
344 (2.2%) Name of page title (see details)
274 (1.7%) Name of page title (see details)
267 (1.7%) Name of page title (see details)
240 (1.5%) Name of page title (see details)
237 (1.5%) Name of page title (see details)
207 (1.3%) Name of page title (see details)
195 (1.2%) Name of page title (see details)
181 (1.1%) Name of page title (see details)
174 (1.1%) Name of page title (see details)

Top Countries:

10172 (75%) The United States see details
917 (6.8%) Canada see details
278 (2%) Australia see details
275 (2%) Russian Federation see details
133 (1%) The United Kingdom see details
117 (0.9%) Germany see details
77 (0.6%) Ukraine see details
72 (0.5%) Norway see details
70 (0.5%) Brazil see details
64 (0.5%) Israel see details

Top Cities:

113 (0.9%) Los Angeles, CA, USA see details
105 (0.8%) Chicago, IL, USA see details
93 (0.7%) Moscow, Russian Federation see details
87 (0.7%) Houston, TX, USA see details
81 (0.6%) New York, NY, USA see details
73 (0.6%) Toronto, Canada see details
63 (0.5%) Brooklyn, NY, USA see details
63 (0.5%) Sydney, Australia see details
57 (0.4%) Melbourne, Australia see details
57 (0.4%) Philadelphia, PA, USA see details

Top Regions:

1110 (8.7%) CA, USA see details
618 (4.8%) NY, USA see details
597 (4.7%) TX, USA see details
538 (4.2%) FL, USA see details
442 (3.5%) PA, USA see details
432 (3.4%) IL, USA see details
367 (2.9%) OH, USA see details
360 (2.8%) MI, USA see details
317 (2.5%) ON, Canada see details
285 (2.2%) WA, USA see details

Top Web Browsers:

4655 (34.1%) Internet Explorer 8.0 see details
2941 (21.5%) Firefox 3.6 see details
2638 (19.3%) Internet Explorer 7.0 see details
1245 (9.1%) Google Chrome 8.0 see details
779 (5.7%) Safari 5.0 see details
293 (2.1%) Firefox 3.5 see details
193 (1.4%) Internet Explorer 6.0 see details
179 (1.3%) Opera 10.0 see details
153 (1.1%) Firefox 3.0 see details
132 (1%) Safari 4.0 see details

Posted Tue Jan 25 2011 11:12p by serenex***


How can i create a report (for email) for a unique link (index.php?id=1 // index.php?id=2 // and so on)?
any help is greatly appreciated.

Posted Fri May 13 2011 3:30p by ***


You must be logged in to your account to post!