Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Aggregates number of page views from individual URLS based on arguments in GA. Uses CSV.
import operator
import csv
import sys
import re
source = sys.argv[1]
output = sys.argv[2]
column = sys.argv[3]
reader = csv.reader(open(source, 'rb'), delimiter=',', quotechar='"')
index = 0
keys = {}
for row in reader:
url = row[0]
count = row[1]
key ='(%s=)(\w+)(&)' % column, url).group(2).lower()
if key not in keys:
keys[key] = int(count)
keys[key] += int(count)
sorted_rows = sorted(keys.iteritems(), key=operator.itemgetter(1))
writer = csv.writer(open(output, "w"), delimiter=",", quotechar='"')
for row in sorted_rows:
key = row[0]
val = row[1]
writer.writerow([key, val])
print row
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment