Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
Added a basic Piwik tracker for analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
ttavenner committed Oct 5, 2014
1 parent fa5ed3c commit c4b16bc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 17 deletions.
32 changes: 15 additions & 17 deletions API.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import mongolab
import json
import os
import re
from math import radians, cos, sin, atan2, sqrt
from bson.objectid import ObjectId
from flask import Flask, Response, url_for, request, current_app, render_template
from functools import wraps
from collections import OrderedDict
from datetime import datetime
from threading import Thread
import os

from flask import Flask, Response, url_for, request, current_app, render_template
from functools import wraps
from livesdataexporter import LivesDataExporter

from piwik import Tracker
from threading import Thread

app = Flask(__name__)
app.debug = False
Expand All @@ -21,6 +19,13 @@
except ValueError:
print "Could not connect to database"

piwik = Tracker()

@app.before_request
def before_request():
if piwik:
piwik.track(request)


def support_jsonp(f):
"""Wraps JSONified output for JSONP"""
Expand Down Expand Up @@ -105,16 +110,9 @@ def api_vendors():
{'type': "Point",
'coordinates': [ float(request.args.get('lng')), float(request.args.get('lat'))]},
'$maxDistance': int(request.args.get('dist'))}}
data = db.va.find(query,
{'name': 1,
'address': 1,
'city': 1,
'locality': 1,
'category': 1,
'type': 1,
'score': 1,
'slug': 1,
'geo.coordinates': 1}).limit(limit)

data = db.va.find(query, output).limit(limit)

if data.count() == 0:
resp = json.dumps({'status': '204', 'message': 'no results returned'})
else:
Expand Down
45 changes: 45 additions & 0 deletions piwik.py
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)


0 comments on commit c4b16bc

Please sign in to comment.