pythonjsonpython-requestssnapchat

Snapchat Marketing API Does Not Return for List Values Requests


I am trying to build a pipeline on Snapchat Marketing API to fetch campaign insight data under the ad account but when I want to run the following python script, It returns the response only with country dimension (do not return os dimension) and only impression metric from the fields list (do not return spend, swipes, total_installs metrics). I think the problem is because of the list values, it takes only the first ones but, I could not solve it.

def post_snapchat_campaign_data(adAccount_id, access_token, granularity, breakdown, report_dimension, start_time, end_time, fields):
   url = "https://adsapi.snapchat.com/v1/adaccounts/{}/stats".format(adAccount_id)

   params = {
        "granularity": granularity,
        "breakdown": breakdown,
        "report_dimension": report_dimension,
        "start_time": start_time,
        "end_time": end_time,
        "fields": fields
}
   headers = {'Authorization': access_token}
   response = requests.request("GET", url, headers = headers, params=params)
   return response



def get_snapchat_campaign_data(adAccount_id, access_token):
   response = post_snapchat_campaign_data(
                    adAccount_id = adAccount_id,
                    access_token = access_token,
                    granularity = "DAY",
                    breakdown = "campaign",
                    report_dimension = ["country", "os"],
                    start_time = "2022-02-02",
                    end_time = "2022-02-03",
                    fields = ["impressions", "spend", "swipes", "total_installs"]
                )

   return response.json()

Solution

  • Your list of multiple values for a single value should not be passed as a list list, instead pass as a string separated by coma. So your function would look like:

    def get_snapchat_campaign_data(adAccount_id, access_token):
                    response = post_snapchat_campaign_data(
                    adAccount_id = adAccount_id,
                    access_token = access_token,
                    granularity = "DAY",
                    breakdown = "campaign",
                    report_dimension = ["country,os"],
                    start_time = "2022-02-02",
                    end_time = "2022-02-03",
                    fields = ["impressions,spend,swipes,total_installs"]
                )
    
        return response.json()