criteo / criteo-python-marketing-sdk

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

SellersV2StatsApi returns empty #5

Closed tzercinhk closed 4 years ago

tzercinhk commented 4 years ago

Hi,

Just started to use this api. Tried several of endpoints and they worked perfectly.

But this endpoint(in the subject) returns zero rows, both from web and from python sdk i do not get why. Tried both a lot of times, with a lot of different criteria set. Everytime I get 0 rows.

For example in the below code, when i run script, self.get_camp_data() returns campaigns list which is expected. Also self.criteo_stats_2() returns data. But self.criteo_stats() returns this ("{'columns': ['campaignId', 'sellerId', 'sellerName', 'day', 'impressions', " "'clicks', 'cost', 'saleUnits', 'revenue', 'cr', 'cpo', 'cos', 'roas'], " "'data': [], 'rows': 0, 'links': {}}").

By the way, I have 2 active campaigns there should be some data. In addition response_code is 200.

Is there any way that my account cannot authorized to use this data/apis etc?

from __future__ import print_function
import time
import criteo_marketing
from criteo_marketing.rest import ApiException
from pprint import pprint
import json
import re

class CriteoApiHk(object):

    def __init__(self):
        # breakpoint()
        with open('auth/criteo.json','r') as authfile:
            self.auth = json.load(authfile)

        self.get_token()
        self.get_camp_data()
        self.criteo_stats()
        self.criteo_stats_2()

    def get_token(self):
        # breakpoint()
        api_instance = criteo_marketing.AuthenticationApi()
        try:
            self.auth_api_response = api_instance.o_auth2_token_post(client_id=self.auth.get("me").get("client_id"), 
                                                                        client_secret= self.auth.get("me").get("client_secret"),
                                                                        grant_type='client_credentials')
        except ApiException as e:
            print("Exception when calling AuthenticationApi->o_auth2_token_post: %s\n" % e)

    def get_camp_data(self):
        # breakpoint()
        #config
        configuration = criteo_marketing.Configuration()
        configuration.host = self.auth.get("host_add")

        #api
        api_instance = criteo_marketing.AdvertisersApi(criteo_marketing.ApiClient(configuration))
        advertiser_id = self.auth.get("advts_id")  
        authorization = 'Bearer {0}'.format(self.auth_api_response.to_dict().get("access_token"))

        try:
            api_response = api_instance.get_campaigns(advertiser_id, authorization)
        except ApiException as e:
            print("Exception when calling AdvertisersApi->get_campaigns: %s\n" % e)

        breakpoint()

        serialized = [camp.to_dict() for camp in api_response]
        result = {"api":serialized}

        with open('data.json', 'w') as output:
            json.dump(result, output, indent=4)

    def criteo_stats(self):
        configuration = criteo_marketing.Configuration()
        configuration.host = self.auth.get("host_add")

        #api
        api_instance = criteo_marketing.SellersV2StatsApi(criteo_marketing.ApiClient(configuration)) 
        authorization = 'Bearer {0}'.format(self.auth_api_response.to_dict().get("access_token"))

        #vars
        start_date = '2020-05-18'
        end_date = '2020-05-19'
        count = 56

        try:
            # Get stats by seller-campaign.
            api_response = api_instance.seller_campaigns(authorization, 
                                                            start_date=start_date, 
                                                            end_date=end_date, 
                                                            count=count)
            pprint(api_response)
        except ApiException as e:
            print("Exception when calling SellersV2StatsApi->seller_campaigns: %s\n" % e)

    def criteo_stats_2(self):
        # breakpoint()
        token = 'Bearer ' + self.auth_api_response.to_dict().get("access_token")

        configuration = criteo_marketing.Configuration()
        configuration.host = self.auth.get("host_add")

        stats_api = criteo_marketing.StatisticsApi(criteo_marketing.ApiClient(configuration))
        stats_query_message = criteo_marketing.StatsQueryMessageEx(
                                                                    report_type="CampaignPerformance",
                                                                    dimensions=["CampaignId"],
                                                                    metrics=["ConversionRate"],
                                                                    start_date="2020-05-01",
                                                                    end_date="2020-05-31",
                                                                    format="json"
        )

        # Use the method with 'with_http_info' if you want to retrieve the filename
        # Otherwise, you can directly call the get_stats method and write the return value to a file

        [response_content, http_code, response_headers] = stats_api.get_stats_with_http_info(token, stats_query_message)
        breakpoint()
        if 200 == http_code:
            content_disposition = response_headers["Content-Disposition"]
            if content_disposition:
                filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', content_disposition).group(1)

                # $> file Extract_20190101_20190131.csv
                # Extract_20190101_20190131.csv: UTF-8 Unicode (with BOM) text, with CRLF line terminators
                with open(filename, "w+") as f:
                    f.write(response_content)

if __name__ == "__main__":
    CriteoApiHk()
tzercinhk commented 4 years ago

First I thought I had problems with Statistics API, but solved that. SellersV2StatsAPI problem continues.

I edited the code in the question.

ouvreboite commented 4 years ago

Hello @tzercinhk, could you provide us with the content of your criteo.json file (without your credentials), so that we can reproduce the problem in the same conditions.

tzercinhk commented 4 years ago

Hi,

You can find below. { "advts_id": 10000, "host_add" : "https://api.criteo.com/marketing", "account_email" : "account@email.com", "pass":"password", "me":{ "client_secret" : "client secret", "client_id":" "mapi-client id", "client_mail" : "clientaccount@email.com" } }

ouvreboite commented 4 years ago

Hello @tzercinhk.

After discussing with the Sellers team, it seems the problem comes from their side (some missing data). From what we understand, both the Python SDK and the exposed API is working fine, so we will close this issue.

Could you open an incident using Management Center or by contacting your Criteo contact?