Skip to content

Commit a1028ba

Browse files
committed
Added handling for v3 advanced segment ids which aren't just ints
The old int based advanced segment ids are deprecated. The new segment ids are alphanumeric. I know from experimentation that their form includes alphanumeric and alphanumeric+hyphen. Updated code for both cases. Consolidated tests and fixed broken references In consolidating my test file with the preexisting GA test file, I found a broken dependency to reset_token_store. It looks like it was renamed to reset_default_token_store so I updated this file to point to that. Also added tests around formatting of segment ids to the GA query to this file. **CLN** Removed debugging code I added.
1 parent 3776c84 commit a1028ba

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ Improvements to existing features
186186
- DatetimeIndex (and date_range) can now be constructed in a left- or
187187
right-open fashion using the ``closed`` parameter (:issue:`4579`)
188188
- Python csv parser now supports usecols (:issue:`4335`)
189+
- Added support for Google Analytics v3 API segment IDs that also supports v2 IDs. (:issue:`5271`)
189190

190191
API Changes
191192
~~~~~~~~~~~

pandas/io/ga.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
4. Download JSON secret file and move into same directory as this file
66
"""
77
from datetime import datetime
8+
import re
89
from pandas import compat
910
import numpy as np
1011
from pandas import DataFrame
@@ -359,7 +360,10 @@ def format_query(ids, metrics, start_date, end_date=None, dimensions=None,
359360
[_maybe_add_arg(qry, n, d) for n, d in zip(names, lst)]
360361

361362
if isinstance(segment, compat.string_types):
362-
_maybe_add_arg(qry, 'segment', segment, 'dynamic::ga')
363+
if re.match("^[a-zA-Z0-9]+\-*[a-zA-Z0-9]*$", segment):
364+
_maybe_add_arg(qry, 'segment', segment, 'gaid:')
365+
else:
366+
_maybe_add_arg(qry, 'segment', segment, 'dynamic::ga')
363367
elif isinstance(segment, int):
364368
_maybe_add_arg(qry, 'segment', segment, 'gaid:')
365369
elif segment:

pandas/io/tests/test_ga.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
try:
1212
import httplib2
13+
import pandas.io.ga as ga
1314
from pandas.io.ga import GAnalytics, read_ga
14-
from pandas.io.auth import AuthenticationConfigError, reset_token_store
15+
from pandas.io.auth import AuthenticationConfigError, reset_default_token_store
1516
from pandas.io import auth
1617
except ImportError:
1718
raise nose.SkipTest("need httplib2 and auth libs")
@@ -25,7 +26,7 @@ def test_remove_token_store(self):
2526
with open(auth.DEFAULT_TOKEN_FILE, 'w') as fh:
2627
fh.write('test')
2728

28-
reset_token_store()
29+
reset_default_token_store()
2930
self.assert_(not os.path.exists(auth.DEFAULT_TOKEN_FILE))
3031

3132
@slow
@@ -98,6 +99,26 @@ def test_iterator(self):
9899
except AuthenticationConfigError:
99100
raise nose.SkipTest("authentication error")
100101

102+
def test_v2_advanced_segment_format(self):
103+
advanced_segment_id = 1234567
104+
query = ga.format_query('google_profile_id', ['visits'], '2013-09-01', segment=advanced_segment_id)
105+
assert query['segment'] == 'gaid::' + str(advanced_segment_id), "An integer value should be formatted as an advanced segment."
106+
107+
def test_v2_dynamic_segment_format(self):
108+
dynamic_segment_id = 'medium==referral'
109+
query = ga.format_query('google_profile_id', ['visits'], '2013-09-01', segment=dynamic_segment_id)
110+
assert query['segment'] == 'dynamic::ga:' + str(dynamic_segment_id), "A string value with more than just letters and numbers should be formatted as a dynamic segment."
111+
112+
def test_v3_advanced_segment_common_format(self):
113+
advanced_segment_id = 'aZwqR234'
114+
query = ga.format_query('google_profile_id', ['visits'], '2013-09-01', segment=advanced_segment_id)
115+
assert query['segment'] == 'gaid::' + str(advanced_segment_id), "A string value with just letters and numbers should be formatted as an advanced segment."
116+
117+
def test_v3_advanced_segment_weird_format(self):
118+
advanced_segment_id = 'aZwqR234-s1'
119+
query = ga.format_query('google_profile_id', ['visits'], '2013-09-01', segment=advanced_segment_id)
120+
assert query['segment'] == 'gaid::' + str(advanced_segment_id), "A string value with just letters, numbers, and hyphens should be formatted as an advanced segment."
121+
101122
@slow
102123
@with_connectivity_check("http://www.google.com")
103124
def test_segment(self):

0 commit comments

Comments
 (0)