package MT::Plugin::GoogleAnalyticsWidget; use strict; use warnings; use base qw( MT::Plugin ); use MT; use Data::Dumper; use MT::Util qw( start_end_day epoch2ts ts2epoch format_ts ); use XML::Simple; use lib 'lib'; use Analytics; use vars qw( $VERSION $plugin ); $VERSION = '0.1'; $plugin = MT::Plugin::GoogleAnalyticsWidget->new ({ name => 'GoogleAnalyticsWidget', description => 'Creates a Google Analytics widget in the blog dashboard', version => $VERSION, author_name => 'Apperceptive, LLC', author_link => 'http://www.apperceptive.com/', blog_config_template => 'blog_config.tmpl', settings => MT::PluginSettings->new ([ [ 'analytics_username', { Default => undef, Scope => 'blog' } ], [ 'analytics_password', { Default => undef, Scope => 'blog' } ], [ 'analytics_profile_id', { Default => undef, Scope => 'blog' } ], [ 'analytics_VisitorsOverview_Visits', { Default => undef, Scope => 'blog' } ], [ 'analytics_VisitorsOverview_Visitors', { Default => undef, Scope => 'blog' } ], [ 'analytics_VisitorsOverview_Pageviews', { Default => undef, Scope => 'blog' } ], [ 'analytics_VisitorsOverview_AvgPageviews', { Default => undef, Scope => 'blog' } ], [ 'analytics_VisitorsOverview_TimeOnSite', { Default => undef, Scope => 'blog' } ], [ 'analytics_VisitorsOverview_BounceRate', { Default => undef, Scope => 'blog' } ], [ 'analytics_VisitorsOverview_NewVisitors', { Default => undef, Scope => 'blog' } ], [ 'analytics_TrafficSources_Direct', { Default => undef, Scope => 'blog' } ], [ 'analytics_TrafficSources_Referral', { Default => undef, Scope => 'blog' } ], [ 'analytics_TrafficSources_Search', { Default => undef, Scope => 'blog' } ], [ 'analytics_Content_Pageview', { Default => undef, Scope => 'blog' } ], [ 'analytics_Content_Unique', { Default => undef, Scope => 'blog' } ], [ 'analytics_Content_Bounce', { Default => undef, Scope => 'blog' } ], ]), init_app => { 'MT::App::CMS' => \&init_cms, }, callbacks => { 'MT::App::CMS::template_source.blog_stats' => \&blog_stats_source, } }); MT->add_plugin ($plugin); sub instance { $plugin } sub init_cms { my $app = shift; { local $SIG{__WARN__} = sub {}; my $orig_method = \&MT::App::CMS::generate_dashboard_stats; *MT::App::CMS::generate_dashboard_stats = sub { if (!$orig_method->(@_)) { return; } generate_analytics_stats (@_); } } } sub generate_analytics_stats { my $app = shift; my ($param) = @_; return 1 if (!$app->blog || !$plugin->get_config_value ('analytics_profile_id', 'blog:' . $app->blog->id)); return 1 if (! ( $plugin->get_config_value ('analytics_username', 'blog:' . $app->blog->id) && $plugin->get_config_value ('analytics_password', 'blog:' . $app->blog->id) ) ); my $cache_time = 60 * 60 * 24; # cache for one day my $stat_url = $param->{comment_stat_url}; $stat_url =~ s{comment\.xml$}{}; my @items1 = getItems1($app); my %items2 = getItems2($app); my %analytics_file = (); my %analytics_path = (); foreach my $item (@items1) { $analytics_file{$item} = 'google_analytics_'.$item.'.xml'; $param->{'analytics_'.$item.'_stat_url'} = $stat_url . $analytics_file{$item}; $analytics_path{$item} = File::Spec->catfile ($param->{support_path}, $analytics_file{$item}); } my @dl_reports = (); foreach my $report (keys(%items2)) { foreach my $item (@{$items2{$report}}) { my $analytics_time = ( stat($analytics_path{$item}) )[9] if -f $analytics_path{$item}; if (!$analytics_time || ( time - $analytics_time > $cache_time )) { push(@dl_reports, $report); } } } my %tmp; @dl_reports = grep {!$tmp{$_}++} @dl_reports; foreach my $dl_report (@dl_reports) { my $now = time; my $today = start_end_day ( epoch2ts ($app->blog, $now ) ); my $thirty_days_ago = start_end_day ( epoch2ts ($app->blog, $now - (60 * 60 * 24 * 120)) ); my $a = Analytics->new; $a->login ($plugin->get_config_value ('analytics_username', 'blog:' . $app->blog->id), $plugin->get_config_value ('analytics_password', 'blog:' . $app->blog->id)); my $xml_ref = $a->getReport ($plugin->get_config_value ('analytics_profile_id', 'blog:' . $app->blog->id), format_ts ('%Y%m%d', $thirty_days_ago, $app->blog), format_ts ('%Y%m%d', $today, $app->blog), $dl_report.'Report'); foreach my $item (@{$items2{$dl_report}}) { my @data = @{$xml_ref->{Report}->{Sparkline}->{$item.'Sparkline'}->{PrimaryValue}}; my $this_day = $thirty_days_ago; my %counts = (); foreach my $count (@data) { $counts{substr ($this_day, 0, 8) . 'T00:00:00'} = $count; $this_day = epoch2ts ( $app->blog, ts2epoch ($app->blog, $this_day) + (60 * 60 * 24)); } unless ($app->create_dashboard_stats_file ($analytics_path{$item}, \%counts)) { delete $param->{$analytics_path{$item}}; } } } 1; } sub blog_stats_source { my ($cb, $app, $tmpl) = @_; return if (!$app->blog || !$plugin->get_config_value ('analytics_profile_id', 'blog:' . $app->blog->id)); my @items = getItems1($app); my $new = ''; # generate panel codes $new = ''; foreach my $item (@items) { $new .= q{