criteo / criteo-python-marketing-sdk

Official Python SDK to access the Criteo Marketing API
Apache License 2.0
15 stars 8 forks source link

Hour dimension usage in StatisticsApi returns error. #7

Open UmarIgan opened 4 years ago

UmarIgan commented 4 years ago

Hello, I am trying get daily AdvertiserCost for my marketing reports with criteo and when i use day as dimension i can see all data except today but i want get the most current values for today so after some digging, i found out that there is also hour dimension but when i use it i got message":"The requested dimensions combination is not supported. error. Here is full code including error message:

import criteo_marketing as cm
from criteo_marketing import Configuration
import json
from datetime import date
configuration = Configuration(username=" user_name", password="pass_word")
client = cm.ApiClient(configuration)
auth_api = cm.AuthenticationApi(client)
auth_response = auth_api.o_auth2_token_post(client_id=client.configuration.username,client_secret=client.configuration.password,grant_type="client_credentials")
token = auth_response.token_type + " " + auth_response.access_token

today = date.today().strftime("%Y-%m-%d")

stats_api = cm.StatisticsApi(client)
stats_query_message = cm.StatsQueryMessageEx(dimensions=["Hour"],
            metrics=["AdvertiserCost"],
            start_date="2018-07-10",
            end_date=today,
            currency="TRY",
            format="json")
[response_content, http_code, response_headers]=stats_api.get_stats_with_http_info(token, stats_query_message)
response_content=response_content.replace('\ufeff', '')
json1_data = json.loads(response_content)['Rows']
json1_data 
---------------------------------------------------------------------------
ApiException                              Traceback (most recent call last)
<ipython-input-27-4024c70d078d> in <module>
     19             currency="TRY",
     20             format="json")
---> 21 [response_content, http_code, response_headers]=stats_api.get_stats_with_http_info(token, stats_query_message)
     22 response_content=response_content.replace('\ufeff', '')
     23 json1_data = json.loads(response_content)['Rows']

/usr/local/lib/python3.8/site-packages/criteo_marketing/api/statistics_api.py in get_stats_with_http_info(self, authorization, stats_query, **kwargs)
    261         auth_settings = ['Authorization']  # noqa: E501
    262 
--> 263         return self.api_client.call_api(
    264             '/v1/statistics', 'POST',
    265             path_params,

/usr/local/lib/python3.8/site-packages/criteo_marketing/api_client.py in call_api(self, resource_path, method, path_params, query_params, header_params, body, post_params, files, response_type, auth_settings, async_req, _return_http_data_only, collection_formats, _preload_content, _request_timeout, _host)
    334         """
    335         if not async_req:
--> 336             return self.__call_api(resource_path, method,
    337                                    path_params, query_params, header_params,
    338                                    body, post_params, files,

/usr/local/lib/python3.8/site-packages/criteo_marketing/api_client.py in __call_api(self, resource_path, method, path_params, query_params, header_params, body, post_params, files, response_type, auth_settings, _return_http_data_only, collection_formats, _preload_content, _request_timeout, _host)
    166 
    167         # perform request and return response
--> 168         response_data = self.request(
    169             method, url, query_params=query_params, headers=header_params,
    170             post_params=post_params, body=body,

/usr/local/lib/python3.8/site-packages/criteo_marketing/api_client.py in request(self, method, url, query_params, headers, post_params, body, _preload_content, _request_timeout)
    378                                             body=body)
    379         elif method == "POST":
--> 380             return self.rest_client.POST(url,
    381                                          query_params=query_params,
    382                                          headers=headers,

/usr/local/lib/python3.8/site-packages/criteo_marketing/rest.py in POST(self, url, headers, query_params, post_params, body, _preload_content, _request_timeout)
    278     def POST(self, url, headers=None, query_params=None, post_params=None,
    279              body=None, _preload_content=True, _request_timeout=None):
--> 280         return self.request("POST", url,
    281                             headers=headers,
    282                             query_params=query_params,

/usr/local/lib/python3.8/site-packages/criteo_marketing/rest.py in request(self, method, url, query_params, headers, body, post_params, _preload_content, _request_timeout)
    237 
    238         if not 200 <= r.status <= 299:
--> 239             raise ApiException(http_resp=r)
    240 
    241         return r

ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'cache-control': 'private', 'content-length': '68', 'content-type': 'application/json; charset=utf-8', 'x-aspnet-version': '4.0.30319', 'date': 'Tue, 18 Aug 2020 06:59:55 GMT'})
HTTP response body: {"message":"The requested dimensions combination is not supported."}
bubianchi commented 4 years ago

@pytmar When you request with the "Hour" dimension, you shall also ask for "Day" dimension, as the "hour" column in the response will contain only an hour range e.g. "5:00 PM - 5:59 PM" without specifying the "day" they belong to.

UmarIgan commented 4 years ago

Thank you for your answer. So what dimension should i use getting the metrics for current moment?

bubianchi commented 4 years ago

The dimensions specify how to "split" data in rows: one row of metric for each combination of dimension values. The time range for the metrics is specified by the boundaries "StartDate" and "EndDate". These dates are respectively truncated to "begin of the day"and "end of the day. The minimal range is then one day. For instance: startDate = "2020-08-18" and endDate = "2020-08-18" for the whole day of August the 18th 2020.

Note: the metrics data are refreshed and are only available after several hours. The latest data you can get are probably 4- to 6-hour old.

UmarIgan commented 4 years ago

Thank you, when i set endDate to today i can't see current metrics and if i set endDate to let's say August the 18th 2021 i still can't see any matrics for today. From your answer i should get data belongs to 4-6 ago before this current moment, this what i understood am i right? but still i can't see.