1

GA requires a string for start_date and end_date. I have a text file that has a list dates that I want to iterate through. When I try the following code:

    def get_results(service, profile_id):


       for x in open("C:\\E\\dates.txt"):
           print type(x)
           print x
           # Use the Analytics Service Object to query the Core Reporting 
           return service.data().ga().get(
           ids='ga:' + profile_id,
           start_date=x, # YYYY/MM/DD
           end_date=x,
           dimensions='ga:date,ga:deviceCategory',
           metrics='ga:sessions,ga:users,ga:pageviews').execute()

I get the error: Arg, there was an API error : 400 : Invalid value '2015-02-11 '. Values must match the following regular expression: '[0-9]{4}-[0-9]{2}-[0-9]{2}|today|yesterday|[0-9]+(daysAgo)'

I checked the type(x) and it returns type 'str' in powershell. If I am returning a string, why is it not being accepted as a valid start/end date?

When I run the code:

    def get_results(service, profile_id):
       x = '2015-02-01'
       print x
       # Use the Analytics Service Object to query the Core Reporting API
       return service.data().ga().get(
       ids='ga:' + profile_id,
       start_date=x, # YYYY/MM/DD
       end_date=x,
       dimensions='ga:date,ga:deviceCategory',
       metrics='ga:sessions,ga:users,ga:pageviews').execute()

I do not receive an error and the type(x) returns type 'str'

Thank you for any help.

Jahmul14
  • 329
  • 4
  • 18
  • I don't know if that is simply a typo, but the date in the error message has a whitespace character at the end (which mean it does not match the regex). – Eike Pierstorff Feb 14 '15 at 08:29
  • You are right, I used X.strip() to remove the whitespace from both ends of the string and it worked. I cant get it to loop through the the other dates in the text file now, it is only using the first one and ends. Any suggestions? – Jahmul14 Feb 14 '15 at 21:10
  • I don't know python, but in most languages a return statement would terminate the loop after the first result (here is a question about return statements in loops, might be that this applies here:http://stackoverflow.com/questions/5864166/return-statement-in-for-loops) – Eike Pierstorff Feb 15 '15 at 10:43

0 Answers0