Permalink
Browse files

bug fix passing in months from command line and csv now sorts ga data…

… by most visits desc
  • Loading branch information...
1 parent 6172af4 commit 44c2b641f981500cac669b8ae5da005b8047d2e1 @andrewschoen committed May 1, 2011
Showing with 4 additions and 2 deletions.
  1. +4 −2 cms_redirects/management/commands/redirect_csv.py
@@ -1,6 +1,7 @@
import StringIO
import csv
import datetime
+import operator
from django.core.management.base import BaseCommand, CommandError
from django.contrib.sites.models import Site
@@ -63,10 +64,11 @@ def execute(self, *args, **options):
connection = Connection(options["ga_user"], options["ga_pwd"])
account = connection.get_account(options["ga_profile"])
end_date = datetime.date.today()
- num_months = options["num_analytics_months"]
+ num_months = int(options["num_analytics_months"])
start_date = end_date - datetime.timedelta(num_months*365/12)
data = account.get_data(start_date=start_date, end_date=end_date, dimensions=['pagepath'], metrics=['visits'])
- for url, visits in data.dict.iteritems():
+ sorted_data = sorted(data.dict.iteritems(), key=operator.itemgetter(1), reverse=True)
+ for url, visits in sorted_data:
writer.writerow([csv_safe(url),'',''])
else:
[writer.writerow(['','','']) for row in range(0,num_rows)]

0 comments on commit 44c2b64

Please sign in to comment.