This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a basic Piwik tracker for analytics
- Loading branch information
Showing
2 changed files
with
60 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from urlparse import urlparse | ||
import config | ||
import random | ||
import requests | ||
|
||
|
||
class Tracker: | ||
|
||
def __init__(self): | ||
c = config.load() | ||
|
||
self.params = {} | ||
try: | ||
self.endpoint = 'http://' + c['piwik_endpoint'] + '/piwik.php' | ||
except AttributeError: | ||
print 'Piwik endpoint not defined!' | ||
|
||
try: | ||
self.params['idsite'] = c['piwik_siteid'] # ID of the site being tracked | ||
except AttributeError: | ||
print 'Piwik site ID not defined!' | ||
|
||
try: | ||
self.params['token_auth'] = c['piwik_tokenauth'] # API token auth from Piwik | ||
except AttributeError: | ||
print 'Piwik token auth not defined!' | ||
|
||
self.params['rec'] = 1 # Required field. (always eq. 1) | ||
self.params['apiv'] = 1 # API version (always eq. 1) | ||
|
||
def track(self, data): | ||
if 'User-Agent' in data.headers: | ||
self.params['ua'] = data.headers['User-Agent'] | ||
if data.referrer: | ||
self.params['urlref'] = data.referrer | ||
|
||
self.params['url'] = data.url # full URL of the current action | ||
self.params['action_name'] = urlparse(data.url).path.replace('/','') | ||
self.params['_id'] = '' # unique visitor id | ||
self.params['rand'] = random.randint(0, 999999) # random ID for current request | ||
self.params['cip'] = data.remote_addr | ||
|
||
r = requests.get(self.endpoint, params=self.params) | ||
|
||
|