public
Last active

show common statistic from Yandex.metrika for last 7 days

  • Download Gist
metrika-show-common-stat.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
#!/usr/bin/perl
 
=head1 NAME
 
metrika-show-common-stat.pl
 
=head1 DESCRIPTION
 
Show common statistic from Yandex.metrika
 
uses API from: http://api.yandex.com/metrika/
 
install from: https://gist.github.com/msoap/3772872
 
=head1 SYNOPSIS
 
metrika-show-common-stat.pl options
--token=XXXXX -- OAuth totken, required
--counter-id=NNNNN -- counter ID, required
--days=N -- show stat for last N days (default is 7 days)
--short -- show short stat, only visits and visitors
 
or read config from ~/.config/metrika-show-common-stat.cfg
 
=cut
 
use strict;
use warnings;
use v5.10.0;
 
use Getopt::Long;
use LWP::Simple;
use POSIX qw/strftime/;
use JSON;
use Term::ANSIColor;
 
use utf8;
use open ":std" => ":utf8";
 
our $BASE_URL = "https://api-metrika.yandex.com/%s.json?id=%s&pretty=1&oauth_token=%s&date1=%s&date2=%s";
our $CONFIG_FILENAME = "$ENV{HOME}/.config/metrika-show-common-stat.cfg";
our $DEFAULT_DAYS = 7;
 
# ------------------------------------------------------------------------------
sub main {
my $config = {days => $DEFAULT_DAYS};
 
if (-f $CONFIG_FILENAME && -r $CONFIG_FILENAME) {
open my $FH, '<', $CONFIG_FILENAME or die "Error open file: $!\n";
for my $row (<$FH>) {
next if $row =~ /^\s*#/;
chomp $row;
my ($k, $v) = split /\s*=\s*/, $row, 2;
$config->{$k} = $v;
}
close $FH;
}
 
GetOptions(
'token=s' => \$config->{token},
'counter-id=i' => \$config->{'counter-id'},
'days=i' => \$config->{days},
'short' => \$config->{short},
'help' => sub {
print "$0 --token=XXXXX --counter-id=NNNNN [--days=N --short --help]\n";
exit 0;
}
);
 
die("need --token and --counter-id options\n") unless defined $config->{token} && defined $config->{'counter-id'};
 
for my $day (reverse 0 .. $config->{days}) {
my $date_param = strftime("%Y%m%d", localtime(time() - $day * 3600 * 24));
my $date = strftime("%Y-%m-%d (%a)", localtime(time() - $day * 3600 * 24));
 
my $get_stat = sub($) {
my $type = shift;
return get_stat($type, $date_param, $config->{'counter-id'}, $config->{token});
};
 
print colored($date, "yellow") . "\n";
 
my $stat = $get_stat->('stat/traffic/summary');
if (exists $stat->{errors} && $stat->{errors}->[0]->{code}) {
printf " -- %s --\n",
{'ERR_NO_DATA' => "no visits",
'ERR_NO_NET' => "network priblem",
'ERR_JSON' => "json parsing error",
}->{ $stat->{errors}->[0]->{code} }
|| "error: " . $stat->{errors}->[0]->{code};
next;
}
 
my ($new_visitors, $visitors, $visits) = (map {$stat->{totals}->{$_}} qw/new_visitors visitors visits/);
print " ";
print format_val('visitors' => $visitors) . ' ';
print format_val('new visitors' => $new_visitors) . ' ';
print format_val('visits' => $visits);
 
if ($config->{short}) {
print "\n";
next;
}
 
# by hours
my $stat_hourly = $get_stat->('stat/traffic/hourly');
my $hours = join(', ', map {$_->{hours}} grep {$_->{avg_visits}} @{$stat_hourly->{data}});
print " " . format_val('hours' => $hours) . "\n";
 
# by sources
my $stat_sources = $get_stat->('stat/sources/summary');
print " " . format_val('where from' => join(', ', map {"$_->{name}: " . colored($_->{visits}, 'bold blue')} @{$stat_sources->{data}}));
 
# by geo
my $stat_geo = $get_stat->('stat/geo');
print " " . format_val('geo' => join(', ', map {"$_->{name}: " . colored($_->{visits}, 'bold blue')} @{$stat_geo->{data}})) . "\n";
 
# by os
my $stat_os = $get_stat->('stat/tech/os');
print " " . format_val('OS' => join(', ', map {"$_->{name}: " . colored($_->{visits}, 'bold blue')} @{$stat_os->{data}}));
 
# by browsers
my $stat_browsers = $get_stat->('stat/tech/browsers');
print " " . format_val('browsers' => join(', ', map {"$_->{name}/$_->{version}: " . colored($_->{visits}, 'bold blue')} @{$stat_browsers->{data}}));
 
# by display
my $stat_display = $get_stat->('stat/tech/display');
print " " . format_val('screen' => join(', ', map {"$_->{name}: " . colored($_->{visits}, 'bold blue')} @{$stat_display->{data}})) . "\n";
}
}
 
# ------------------------------------------------------------------------------
sub get_stat($$$$) {
my ($type, $date, $counter_id, $token) = @_;
 
my $url = sprintf($BASE_URL, $type, $counter_id, $token, $date, $date);
my $json = get($url);
return {errors => [{code => 'ERR_NO_NET'}]} unless $json;
 
my $stat = eval {from_json($json)};
return {errors => [{code => 'ERR_JSON'}]} unless ref($stat) eq 'HASH';
 
return from_json($json);
}
 
# ------------------------------------------------------------------------------
sub format_val($$;$) {
my ($title, $value, $color) = @_;
 
$color //= 'green';
return colored("$title: ", 'green') . $value;
}
 
# ------------------------------------------------------------------------------
main();

Show common stat from Yandex.Metrika

SYNOPSIS

metrika-show-common-stat.pl options
  --token=XXXXX       -- OAuth totken, required
  --counter-id=NNNNN  -- counter ID, required
  --days=N            -- show stat for last N days (default is 7 days)
  --short             -- show short stat, only visits and visitors

or read config from ~/.config/metrika-show-common-stat.cfg

INSTALL

curl https://gist.github.com/msoap/3772872/raw/dae78c8704879351fc60b1326812878bfb2b9ee4/metrika-show-common-stat.pl > ~/bin/metrika-show-common-stat.pl
chmod a+x ~/bin/metrika-show-common-stat.pl

PREVIEW

yandex-weather.coffee.screenshot.png

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.