Commits

Arun Karunagath committed 122293b Merge

Merge pull request #5 from paperlabs/upstream-fixes

flexible time-stamp handling, fix for utmz parsing, one more test case: Thanks for @nsitarz (Nick Sitarz).

  • Participants
  • Parent commits cf2ddc7, a8be5d9

Comments (0)

Files changed (4)

File pyga/entities.py

         return obj
 
     def extract_from_utmz(self, utmz):
-        params = utmz.split(Campaign.CAMPAIGN_DELIMITER)
-        parts = params[0].split('.', 5)
+        parts = utmz.split('.', 4)
+
         if len(parts) != 5:
             raise ValueError('The given "__utmz" cookie value is invalid.')
 
-        self.creation_time = datetime.utcfromtimestamp(float(parts[1]))
+        self.creation_time = utils.convert_ga_timestamp(parts[1])
         self.response_count = int(parts[3])
-        params.insert(0, parts[4])
+        params = parts[4].split(Campaign.CAMPAIGN_DELIMITER)
 
         for param in params:
             key, val = param.split('=')
             raise ValueError('The given "__utmb" cookie value is invalid.')
 
         self.track_count = int(parts[1])
-        self.start_time = datetime.utcfromtimestamp(float(parts[3]))
+        self.start_time = utils.convert_ga_timestamp(parts[3])
 
         return self
 
             raise ValueError('The given "__utma" cookie value is invalid.')
 
         self.unique_id = int(parts[1])
-        self.first_visit_time = datetime.utcfromtimestamp(float(parts[2]))
-        self.previous_visit_time = datetime.utcfromtimestamp(float(parts[3]))
-        self.current_visit_time = datetime.utcfromtimestamp(float(parts[4]))
+        self.first_visit_time = utils.convert_ga_timestamp(parts[2])
+        self.previous_visit_time = utils.convert_ga_timestamp(parts[3])
+        self.current_visit_time = utils.convert_ga_timestamp(parts[4])
         self.visit_count = int(parts[5])
 
         return self

File pyga/requests.py

 
         headers = {}
         headers['Host'] = self.config.endpoint.split('/')[2]
-        headers['USER_AGENT'] = self.user_agent
-        headers['X_FORWARDED_FOR'] = self.x_forwarded_for and  self.x_forwarded_for or ''
+        headers['User-Agent'] = self.user_agent
+        headers['X-Forwarded-For'] = self.x_forwarded_for and  self.x_forwarded_for or ''
 
         if use_post:
             # Don't ask me why "text/plain", but ga.js says so :)

File pyga/utils.py

 import re
 import urllib
 import os
+from datetime import datetime
 
 __author__ = "Arun KR (kra3) <the1.arun@gmail.com>"
 __license__ = "Simplified BSD"
 RE_GA_ACCOUNT_ID = re.compile(r'^(UA|MO)-[0-9]*-[0-9]*$')
 RE_FIRST_THREE_OCTETS_OF_IP = re.compile(r'^((\d{1,3}\.){3})\d{1,3}$')
 
+def convert_ga_timestamp(timestamp_string):
+    timestamp = float(timestamp_string)
+    if timestamp > ((2 ** 31) - 1):
+        timestamp /= 1000
+    return datetime.utcfromtimestamp(timestamp)
+
 def get_32bit_random_num():
     return randint(0, 0x7fffffff)
 

File tests/utils.py

+import unittest
+import sys
+sys.path.insert(0, '..')
+
+from pyga import utils
+
+
+class TestAnonymizeIp(unittest.TestCase):
+    def test_with_no_ip(self):
+    	self.assertEqual("", utils.anonymize_ip(""))
+
+    def test_with_valid_ip(self):
+    	self.assertEqual("1.2.3.0", utils.anonymize_ip("1.2.3.4"))
+    	self.assertEqual("192.168.137.0", utils.anonymize_ip("192.168.137.123"))
+
+
+if __name__ == '__main__':
+	unittest.main()