[Python] how to request date and location in snapchat ads api?

Discussão em 'Python' iniciado por Stack, Julho 1, 2025.

  1. Stack

    Stack Membro Participativo

    i'm trying to request in snapchat ads api the result of a campaign by date (for example yesterday) and sorted by the country.

    I find the request for the date

    "https://adsapi.snapchat.com/v1/campaigns/["id"]/stats?granularity=DAY&start_time=["date_start"]&end_time=["date_end"]


    And the request for the country

    "https://adsapi.snapchat.com/v1/campaigns/["id"]/stats?granularity=LIFETIME&dimension=GEO&pivots=country"


    this is my code i'm just requesting by country

    r = requests.get("https://adsapi.snapchat.com/v1/organizations/" + organization_id + "/adaccounts", headers = data)

    #get all the add accounts
    ad_list = json.loads(r.text)
    ad_list = ad_list["adaccounts"]
    account_list = {}
    for obj in ad_list:
    test = obj["adaccount"]
    account_list[test["name"]] = {}
    account_list[test["name"]]["id"] = test["id"]
    account_list[test["name"]]["timezone"] = test["timezone"]

    #get all the add accounts campaign's
    for key in account_list:
    r = requests.get("https://adsapi.snapchat.com/v1/adaccounts/" + account_list[key]["id"] + "/campaigns", headers = data)
    ad_id = json.loads(r.text)
    ad_id = ad_id["campaigns"]

    for obj in ad_id:
    test = obj["campaign"]
    account_list[key][test["name"]] = {}
    account_list[key][test["name"]]["type"] = "campaign"
    account_list[key][test["name"]]["id"] = test["id"]

    #get campaign stats by country
    try:
    if (req == "1"):
    r = requests.get("https://adsapi.snapchat.com/v1/campaigns/" + test["id"] + "/stats?granularity=LIFETIME&dimension=GEO&pivots=country&fields=impressions,swipes,spend", headers = data)
    stat = json.loads(r.text)
    stat = stat["lifetime_stats"]
    stat = stat[0]["lifetime_stat"]
    dic = stat["dimension_stats"]
    for country in dic:
    account_list[key][test["name"]][country["country"]] = {}
    for key2 in country:
    account_list[key][test["name"]][country["country"]][key2] = country[key2]
    except:
    continue


    But whatever i'm trying i can't find a way to request the country's result between two date. Is there a solution for this problem? Thanks in Advance.

    Continue reading...

Compartilhe esta Página