Robert Johnstone
Robert Johnstone

Reputation: 21

How to get a Facebook Ad Account's Conversions using the Java FB Business SDK?

I'm trying to get the clicks, spend, and conversions from an Ad Account over a certain time frame, so I downloaded sample code from FB to do so. After a few adjustments I was able to get it running, but it only prints the clicks and spend.

I believe it has to do with the clicks and spend being listed as "numeric Strings", while the conversions are listed as "list" (see here: https://developers.facebook.com/docs/marketing-api/insights/parameters#fields). I'm not sure how to get this to print the value, when I add .toString() to the conversions, it prints this: com.facebook.ads.sdk.AdAccount$APIRequestGetInsights@5ccd43c2.

This is the code I have currently:

import com.facebook.ads.sdk.*;

import com.facebook.ads.sdk.AdAccount.APIRequestGetInsights;

import java.io.File;

import java.util.Arrays;

import java.util.List;

public class SAMPLE_CODE {

public static void main (String args[]) throws APIException {

String access_token = "xxx";

String ad_account_id = "xxx";

String app_secret = "xxx";

String app_id = "xxx";

APIContext context = new APIContext(access_token, app_secret).enableDebug(true);

AdAccount account = new AdAccount(ad_account_id, context);

account.getInsights()

.setLevel(AdsInsights.EnumLevel.VALUE_ACCOUNT)

.setFiltering("[]")

.setBreakdowns(Arrays.asList())

.setTimeRange("{\"since\":\"2019-07-15\",\"until\":\"2019-07-15\"}")

.requestField("clicks")

.requestField("spend")

.requestField("conversions")

.execute();

}

}

Upvotes: 0

Views: 707

Answers (1)

Robert Johnstone
Robert Johnstone

Reputation: 21

To anyone else with this issue, take a look at this link: https://www.damiengonot.com/blog/guide-facebook-insights-api?fbclid=IwAR0l6KwycwrzInLsBlV_0HqLm8PJPPyorcRpAcc6B1IYSfluoJnRVVE3Iso

For some reason, in order to see your conversions you can't use the conversions parameter, you need to use the actions parameter, which returns all of the ActionAdsStats. Not sure why FB does this but that's how you have to do it.

Upvotes: 1

Related Questions