Skip to content

Commit

Permalink
Item12548: fixed tracking unique visitors; enforcing the user id now …
Browse files Browse the repository at this point in the history
…not letting piwik guess it; added control over things to be tracked.

git-svn-id: http://svn.foswiki.org/trunk/PiwikPlugin@16831 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelDaum authored and MichaelDaum committed Jul 16, 2013
1 parent b30dcc5 commit 5fc89dd
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
1 change: 1 addition & 0 deletions data/System/PiwikPlugin.txt
Expand Up @@ -68,6 +68,7 @@ TODO: Let's start this automatically.
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| 15 Jul 2013 | implemented queue manager for better tracking performance; improved control over pages and actions being tracked |
| 14 Jul 2013 | initial release |
| Dependencies: | %$DEPENDENCIES% |
| Home page: | Foswiki:Extensions/%TOPIC% |
Expand Down
2 changes: 2 additions & 0 deletions lib/Foswiki/Plugins/PiwikPlugin.pm
Expand Up @@ -46,6 +46,8 @@ sub initPlugin {

sub completePageHandler {

return unless tracker->isEnabled;

try {
# set all custom variables
if ($Foswiki::cfg{PiwikPlugin}{CustomVariable}) {
Expand Down
10 changes: 10 additions & 0 deletions lib/Foswiki/Plugins/PiwikPlugin/Config.spec
Expand Up @@ -46,4 +46,14 @@ $Foswiki::cfg{PiwikPlugin}{CustomVariable} = [
# This can be directory shared among all virtual hosts when using VirtualHostingContrib.
$Foswiki::cfg{PiwikPlugin}{QueueDir} = '/tmp/PiwikPlugin/queue';

# **STRING**
# A comma-separated list of actions to be tracked automatically. Others are ignored and not reported to Piwik.
# Note that plugins may still call =doTrackPageView()= or =doTrackSiteSearch()= successfuly even though the
# current script action is blocked here.
$Foswiki::cfg{PiwikPlugin}{TrackedActions} = 'edit,view,save';

# **STRING**
# Regular expression matched against the web.topic being tracked. A matching topic won't be recorded to Piwik.
$Foswiki::cfg{PiwikPlugin}{ExcludePattern} = '';

1;
1 change: 1 addition & 0 deletions lib/Foswiki/Plugins/PiwikPlugin/Daemon.pm
Expand Up @@ -147,6 +147,7 @@ sub readRecord {
if (/^(.*?)=(.*)$/) {
$found = 1;
$record{$1} = $2;
#print STDERR "$1=$2\n";
}
}

Expand Down
53 changes: 41 additions & 12 deletions lib/Foswiki/Plugins/PiwikPlugin/Tracker.pm
Expand Up @@ -25,15 +25,13 @@ use JSON();
use File::Temp ();
use File::Path qw(make_path);

use constant DRY => 0; # toggle me
use constant PROFILE => 1; # toggle me

################################################################################
sub new {
my $class = shift;

my $this = bless({
queueDir => $Foswiki::cfg{PiwikPlugin}{QueueDir},
excludePattern => $Foswiki::cfg{PiwikPlugin}{ExcludePattern},
@_
}, $class);

Expand All @@ -42,16 +40,19 @@ sub new {
make_path($this->{queueDir}) || die "Can't create queueDir '$this->{queueDir}'";
}

%{$this->{trackedActions}} = map {$_=>1} split(/\s*,\s*/, $Foswiki::cfg{PiwikPlugin}{TrackedActions} || 'edit,view,save');

return $this;
}

################################################################################
sub init {
my $this = shift;

my $request = Foswiki::Func::getRequestObject;

$this->readVisitorState;

my $request = Foswiki::Func::getRequestObject;
my ($hour, $min, $sec) = Foswiki::Time::formatTime(time(), '$hours:$minutes:$seconds') =~ /^(.*):(.*):(.*)$/;

$this->{params} = {
Expand Down Expand Up @@ -79,6 +80,7 @@ sub init {
if ($Foswiki::cfg{PiwikPlugin}{TokenAuth}) {
$this->{params}{token_auth} = $Foswiki::cfg{PiwikPlugin}{TokenAuth};
$this->{params}{cip} = $this->{currentVisitor}{remoteAddr};
$this->{params}{cid} = $this->{currentVisitor}{id};
#$this->{params}{cdt} = Foswiki::Time::formatTime(time(), '$year-$mo-$day $hours:$minutes:$seconds');# SMELL: does it need to be ... $day, $hours...?
#$this->{params}{cdt} = time();
#print STDERR "cdt=$this->{params}{cdt}\n";
Expand All @@ -90,6 +92,31 @@ sub init {
return $this;
}

################################################################################
sub isEnabled {
my $this = shift;

my $request = Foswiki::Func::getRequestObject;
my $action = $request->action;
unless (defined $this->{trackedActions}{$action}) {
writeDebug("action '$action' isn't tracked");
return 0;
}

if ($this->{excludePattern}) {
my $session = $Foswiki::Plugins::SESSION;
my $webTopic = $session->{webName}.'.'.$session->{topicName};
$webTopic =~ s/\//./g;

if ($webTopic =~ /$this->{excludePattern}/) {
writeDebug("topic '$webTopic' isn't tracked");
return 0;
}
}

return 1;
}

################################################################################
sub doTrackPageView {
my ($this, $web, $topic) = @_;
Expand Down Expand Up @@ -174,7 +201,7 @@ sub setCustomVariable {

$scope = 'visit' unless defined $scope;

writeDebug("setCustomVariable($id, $name, $value, $scope)");
#writeDebug("setCustomVariable($id, $name, $value, $scope)");

die "Parameter id to setCustomVariable should be an integer"
unless defined $id && $id =~ /^\d+$/;
Expand Down Expand Up @@ -222,13 +249,11 @@ sub getVisitorFileName {

################################################################################
sub readVisitorState {
my ($this, $user) = @_;
my ($this, $wikiName) = @_;

my $wikiName = Foswiki::Func::getWikiName($user);
$wikiName ||= Foswiki::Func::getWikiName();
my $file = getVisitorFileName($wikiName);

#writeDebug("file=$file");

my %record = ();
if (-f $file) {
my $data = Foswiki::Func::readFile($file);
Expand All @@ -242,15 +267,17 @@ sub readVisitorState {

my $request = Foswiki::Func::getRequestObject;

$record{user} = $user || 'guest' unless defined $record{user};
$record{wikiName} = $wikiName unless defined $record{wikiName};
$record{remoteAddr} = $request->remoteAddress();
$record{id} = getVisitorId($wikiName);
$record{id} = substr(getVisitorId($wikiName), 0, 16);
$record{count}++;
$record{firstVisit} = time unless defined $record{firstVisit};

$this->{currentVisitor} = \%record;

writeDebug("file=$file, wikiName=$record{wikiName}, id=$record{id}");


return $this;
}

Expand All @@ -267,6 +294,8 @@ sub getVisitorId {
$id = $wikiName;
}

#writeDebug("called getVisitorId($wikiName) id=$id");

return Digest::MD5::md5_hex($id);
}

Expand Down Expand Up @@ -296,7 +325,7 @@ sub queueRecord {
SUFFIX => '.txt',
);

writeDebug("record at '$file'");
#writeDebug("record at '$file'");

while (my ($key, $val) = each %$record) {
print $file "$key=$val\n" or die "Can't write to file '$file'";
Expand Down

0 comments on commit 5fc89dd

Please sign in to comment.