New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It is impossible to retrieve stats by "DAY" granularity due to implementation #70

Closed
yuriybash opened this Issue Jun 29, 2016 · 1 comment

Comments

Projects
None yet
2 participants
@yuriybash
Contributor

yuriybash commented Jun 29, 2016

I am trying to retrieve stats for a set of promoted tweets for a specific, 7-day time range with DAY granularity. Here is what I'm doing (auth info omitted):

import time
import datetime
from twitter_ads.client import Client
from twitter_ads.campaign import LineItem
from twitter_ads.creative import PromotedTweet
from twitter_ads.enum import METRIC_GROUP

CONSUMER_KEY = 'XXXXXX'
CONSUMER_SECRET = 'XXXXXX'
ACCESS_TOKEN = 'XXXXXXX'
ACCESS_TOKEN_SECRET = 'XXXXXXX'
ACCOUNT_ID = '18ce53vumba'

client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

account = client.accounts(ACCOUNT_ID)

ptweet_xids = ['js129', 'ne046']
start_time = datetime.datetime(2016, 2, 26, 5, 0, 0, 0)
end_time = datetime.datetime(2016, 3, 2, 5, 0, 0, 0)
metric_groups = [METRIC_GROUP.BILLING, METRIC_GROUP.ENGAGEMENT]
stats = PromotedTweet.all_stats(account, ptweet_xids, metric_groups, start_time=start_time, end_time=end_time, granularity='DAY')

This is the error I receive:

BadRequest: <BadRequest object at 0x8b07dc0 code=400 details=[{u'message': u"Expect time to be midnight in the account's local timezone for day granularity", u'code': u'INVALID_TIME_WINDOW', u'parameter': u''}, {u'message': u"Expect time to be midnight in the account's local timezone for day granularity", u'code': u'INVALID_TIME_WINDOW', u'parameter': u''}]>

The server expects the start and end times to be in my local timezone (-5 from UTC), but when the params are being generated, the to_time method is called, which strips the hour value from the datetime used. This happens here:

https://github.com/twitterdev/twitter-python-ads-sdk/blob/master/twitter_ads/resource.py#L198-L205
https://github.com/twitterdev/twitter-python-ads-sdk/blob/master/twitter_ads/utils.py#L20-L32

All datetimes are set to midnight UTC, but the server expects the values to be midnight in the local timezone:

In [151]: to_time(datetime.datetime(2016, 2, 26, 5, 0, 0, 0), 'DAY')
Out[151]: '2016-02-26T00:00:00Z'

In [152]: to_time(datetime.datetime(2016, 2, 26, 10, 0, 0, 0), 'DAY')
Out[152]: '2016-02-26T00:00:00Z'

It is therefore effectively impossible to make make any stats requests with DAILY granularity, which is extremely important for our use case. Even if you omit the end_time and start_time and use the defaults in _standard_params, it fails.

In order to make a successful request, the timestamps used in the request must be:

start_time=2016-02-24T05:00:00Z&end_time=2016-03-01T05:00:00Z

I've tested this and it works.

@brandonblack - saw that you wrote the utils module - any chance of getting this fixed soon? I'd be more than happy to open a PR myself to update to_time to take either a time-aware or time-unaware timestamp and format it accordingly.

Thank you!

@juanshishido

This comment has been minimized.

Contributor

juanshishido commented Aug 25, 2016

Fixed with #86.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment