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

Commit

Permalink
[#524] GA improvements - fix dimension3 exceptions. Date now has no d…
Browse files Browse the repository at this point in the history
…ashes and 0 padding.
  • Loading branch information
David Read committed Oct 19, 2016
1 parent f76bb74 commit 1fb31a1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
9 changes: 6 additions & 3 deletions ckanext/dgu/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ def get_ga_custom_dimensions():
organizations = c.userobj.get_groups('organization')
if organizations:
info['dimension2'] = 'publisher' # user_status
info['dimension3'] = [o['name'] for o in organizations]
info['dimension3'] = ' '.join([o.name for o in organizations])
#c.environ['pylons.routes_dict']
controller = c.controller.split(':')[-1]
#controller_action = (controller.split(':')[-1], c.action)
Expand Down Expand Up @@ -614,11 +614,14 @@ def get_ga_custom_dimensions():
return info

def british_date_to_ga_date(british_date):
''' 31/12/2016 -> 2016-12 '''
''' 31/9/2016 -> 2016-09 '''
if not british_date:
return ''
bits = re.split('[^\d]', british_date)[::-1]
return '-'.join(bits[:2])
# pad the month
if bits[1:2]:
bits[1] = bits[1].zfill(2)
return ''.join(bits[:2])

def render_datetime(datetime_, date_format=None, with_hours=False):
'''Render a datetime object or timestamp string as a pretty string
Expand Down
22 changes: 22 additions & 0 deletions ckanext/dgu/tests/lib/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
detect_license_id,
get_license_from_id,
linkify,
british_date_to_ga_date,
)
from ckanext.dgu.plugins_toolkit import c, get_action

Expand Down Expand Up @@ -488,3 +489,24 @@ def test_brackets_excluded_from_link(self):
'Hello '
'(<a href="http://example.com/page.html" target="_blank">'
'http://example.com/page.html</a>) hello')

class TestBritishDateToGaDate(object):
def test_year(self):
assert_equal(british_date_to_ga_date('2012'), '2012')

def test_month_year(self):
# no separator - appears to be the internal GA date type
# keep the '0' to allow sorting by text
assert_equal(british_date_to_ga_date('9/2012'), '201209')

def test_month_year(self):
# no separator - appears to be the internal GA date type
# keep the '0' to allow sorting by text
assert_equal(british_date_to_ga_date('12/2012'), '201212')

def test_day_month_year(self):
# lose the day - simplify for easier analysis
assert_equal(british_date_to_ga_date('1/9/2012'), '201209')

def test_blank(self):
assert_equal(british_date_to_ga_date(''), '')

0 comments on commit 1fb31a1

Please sign in to comment.