Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

Facebook Marketing API implementation (ads management + ads insights) #155

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
cd43c23
Marketing API: Ads support for account operations and account insights.
sebast26 Apr 28, 2015
6cc06ed
Defines campaign operations. Adds support for getting campaign info.
sebast26 May 4, 2015
e60c694
Adds support for creating ad campaign.
sebast26 May 5, 2015
b3b1c7b
Changing the create campaign interface.
sebast26 May 5, 2015
fa50a84
Adds support for updating and deleting ad campaign.
sebast26 May 5, 2015
46d0cfa
Adds possibility to get all campaigns associated with given ad account.
sebast26 May 6, 2015
7c62a3b
Corrects javadoc for getAdAccountCampaigns
sebast26 May 6, 2015
22d8f10
Fix problem with not updating ad campaign status.
sebast26 May 6, 2015
e344342
Clarifies using ad account ID in createAdCampaign methods.
sebast26 May 6, 2015
6ebdd76
Adds AdSet class.
sebast26 May 13, 2015
5fde3fd
Refactoring of AdAccount Operations.
sebast26 May 14, 2015
7afa9d8
Refactoring AdCampaing and CampaignOperations.
sebast26 May 14, 2015
30c68c3
Merge branch 'master' into adset-operations
sebast26 May 14, 2015
9b78b7a
Moves files to right packages.
sebast26 May 14, 2015
6fa8690
Corrects documentation on AccountOperations interface.
sebast26 May 14, 2015
d74bfc1
Correct mapping of AdSet. Adds implementation of createAdSet.
sebast26 May 28, 2015
119dffb
Adds implementation of update and delete operations for AdSet.
sebast26 May 28, 2015
787f7c1
Adds some method for accessing AdSets from different templates.
sebast26 May 29, 2015
bfa9ec8
Adds method for getting info about ad set insights.
sebast26 Jun 1, 2015
e0ad3ce
Adds method for accessing ad campaign insights.
sebast26 Jun 1, 2015
8a5be3d
Adds some small changes after testing on real facebook account.
sebast26 Jun 8, 2015
a4ad96d
Merge branch 'adset-operations'
sebast26 Jun 8, 2015
3d7e4d0
Adds implementation of AdCreative operations.
sebast26 Jun 12, 2015
ed756be
Remove insight operations on AdCreative. Change delete operation to H…
sebast26 Jun 15, 2015
dbafda1
Ads support for Ad Operations.
sebast26 Jun 21, 2015
f3b1e99
Improve Jackson mapping of enums.
sebast26 Jun 21, 2015
bf279bc
Cleaning up things.
sebast26 Jul 6, 2015
6877f76
Adds support for promoted_object in AdSet class.
sebast26 Jul 7, 2015
f4a6411
Adds support for InvalidParameterException.
sebast26 Aug 5, 2015
ed61cdd
Adds two rate limit error codes.
sebast26 Aug 5, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ public class FacebookErrors {
public static final int POKE_OUTSTANDING = 511;
public static final int POKE_RATE = 512;
public static final int POKE_USER_BLOCKED = 513;


// Rate limiting errors
public static final int USER_APP_TOO_MANY_CALLS = 613;

// Ref errors
public static final int REF_SET_FAILED = 700;

Expand Down Expand Up @@ -304,6 +307,9 @@ public class FacebookErrors {
public static final int TEST_ACCOUNTS_INVALID_ID = 2901;
public static final int TEST_ACCOUNTS_CANT_REMOVE_APP = 2902;
public static final int TEST_ACCOUNTS_CANT_DELETE = 2903;

// Ad Level Rate Limit error
public static final int AD_CREATION_LIMIT_EXCEEDED = 1487225;

public static boolean isGeneralError(int code) {
return code < 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,16 @@ public interface GraphApi {
* @param data the data to publish to the connection.
*/
void post(String objectId, String connectionName, MultiValueMap<String, Object> data);


/**
* Updates data of an object.
* Requires appropriate permission to update to the object connection.
* @param objectId the object ID to update.
* @param data the data to update in the object.
* @return true if update was successful
*/
boolean update(String objectId, MultiValueMap<String, Object> data);

/**
* Deletes an object.
* Requires appropriate permission to delete the object.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.springframework.social.facebook.api;

import org.springframework.social.ApiException;

/**
* Exception thrown when one of the supplied parameters is invalid.
*
* @author Sebastian G�recki
*/
public class InvalidParameterException extends ApiException {

public InvalidParameterException(String providerId, String message) {
super(providerId, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package org.springframework.social.facebook.api.ads;

import org.springframework.social.ApiException;
import org.springframework.social.InsufficientPermissionException;
import org.springframework.social.MissingAuthorizationException;
import org.springframework.social.facebook.api.PagedList;
import org.springframework.social.facebook.api.ads.AdUser.AdUserRole;

/**
* Defines operations for working with Facebook Marketing API Ad Account object.
*
* @author Sebastian G�recki
*/
public interface AccountOperations {

static final String[] AD_ACCOUNT_FIELDS = {
"id", "account_id", "account_status", "age", "amount_spent", "balance", "business_city", "business_country_code",
"business_name", "business_state", "business_street", "business_street2", "business_zip", "capabilities",
"created_time", "currency", "daily_spend_limit", "end_advertiser", "funding_source", "funding_source_details",
"is_personal", "media_agency", "name", "offsite_pixels_tos_accepted", "partner", "spend_cap", "timezone_id",
"timezone_name", "timezone_offset_hours_utc", "users", "tax_id_status"
};

static final String[] AD_ACCOUNT_INSIGHT_FIELDS = {
"account_id", "account_name", "date_start", "date_stop", "actions_per_impression", "clicks", "unique_clicks",
"cost_per_result", "cost_per_total_action", "cpc", "cost_per_unique_click", "cpm", "cpp", "ctr", "unique_ctr",
"frequency", "impressions", "unique_impressions", "objective", "reach", "result_rate", "results", "roas",
"social_clicks", "unique_social_clicks", "social_impressions", "unique_social_impressions", "social_reach",
"spend", "today_spend", "total_action_value", "total_actions", "total_unique_actions", "actions",
"unique_actions", "cost_per_action_type", "video_start_actions"
};

/**
* Get all ad accounts for given user.
*
* @param userId the id of an user
* @return the list of {@link AdAccount} objects
* @throws ApiException if there is an error while communicating with Facebook.
* @throws InsufficientPermissionException if the user has not granted "ads_read" or "ads_management" permission.
* @throws MissingAuthorizationException if FacebookAdsTemplate was not created with an access token.
*/
PagedList<AdAccount> getAdAccounts(String userId);

/**
* Get the ad account by given id.
*
* @param accountId the ID of the ad account (account_id)
* @return the {@link AdAccount} object
* @throws ApiException if there is an error while communicating with Facebook.
* @throws InsufficientPermissionException if the user has not granted "ads_read" or "ads_management" permission.
* @throws MissingAuthorizationException if FacebookAdsTemplate was not created with an access token.
*/
AdAccount getAdAccount(String accountId);

/**
* Get all ad campaigns of an ad account.
*
* @param accountId the ID of the ad account (account_id)
* @return the list of {@link AdCampaign} objects
* @throws ApiException if there is an error while communicating with Facebook.
* @throws InsufficientPermissionException if the user has not granted "ads_read" or "ads_management" permission.
* @throws MissingAuthorizationException if FacebookAdsTemplate was not created with an access token.
*/
PagedList<AdCampaign> getAdAccountCampaigns(String accountId);

/**
* Get all users of the ad account.
*
* @param accountId the ID of the ad account, the string act_{ad_account_id}
* @return the list of {@link AdUser} objects
* @throws ApiException if there is an error while communicating with Facebook.
* @throws InsufficientPermissionException if the user has not granted "ads_read" or "ads_management" permission.
* @throws MissingAuthorizationException if FacebookAdsTemplate was not created with an access token.
*/
PagedList<AdUser> getAdAccountUsers(String accountId);

/**
* Add the user to the ad account.
*
* @param accountId the ID of the ad account (account_id)
* @param userId the id of an user (App Scoped User ID)
* @param role the role for the new user in ad account
* @throws ApiException if there is an error while communicating with Facebook.
* @throws InsufficientPermissionException if the user has not granted "ads_management" permission.
* @throws MissingAuthorizationException if FacebookAdsTemplate was not created with an access token.
*/
void addUserToAdAccount(String accountId, String userId, AdUserRole role);

/**
* Remove user's access to an ad account.
*
* @param accountId the ID of the ad account (account_id)
* @param userId the id of an user (App Scoped User ID)
* @throws ApiException if there is an error while communicating with Facebook.
* @throws InsufficientPermissionException if the user has not granted "ads_management" permission.
* @throws MissingAuthorizationException if FacebookAdsTemplate was not created with an access token.
*/
void deleteUserFromAdAccount(String accountId, String userId);

/**
* Get the insight for the ad account in aggregate.
*
* @param accountId the ID of the ad account (account_id)
* @return the {@link AdInsight} object
* @throws ApiException if there is an error while communicating with Facebook.
* @throws MissingAuthorizationException if FacebookAdsTemplate was not created with an access token.
* @throws InsufficientPermissionException if the user has not granted "ads_read" or "ads_management" permission.
*/
AdInsight getAdAccountInsight(String accountId);

/**
* Updates the ad account with information given in adAccount object.
* Currently only updates on ad account name and spend_cap are supported.
*
* @param accountId the ID of the ad account (account_id)
* @param adAccount the ad account object containing updated account information
* @return true if the update succeeded
* @throws ApiException if there is an error while communicating with Facebook.
* @throws MissingAuthorizationException if FacebookAdsTemplate was not created with an access token.
* @throws InsufficientPermissionException if the user has not granted "ads_read" or "ads_management" permission.
*/
boolean updateAdAccount(String accountId, AdAccount adAccount);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package org.springframework.social.facebook.api.ads;

import com.fasterxml.jackson.annotation.JsonCreator;

import java.util.Date;

/**
* @author Sebastian G�recki
*/
public class Ad {
private String id;
private AdStatus status;
private String name;
private BidType bidType;
private BidInfo bidInfo;

private String accountId;
private String adSetId;
private String campaignId;

private String creativeId;
private Targeting targeting;

private Date createdTime;
private Date updatedTime;

public void setStatus(AdStatus status) {
this.status = status;
}

public void setName(String name) {
this.name = name;
}

public void setBidInfo(BidInfo bidInfo) {
this.bidInfo = bidInfo;
}

public void setAdSetId(String adSetId) {
this.adSetId = adSetId;
}

public void setCreativeId(String creativeId) {
this.creativeId = creativeId;
}

public String getId() {
return id;
}

public AdStatus getStatus() {
return status;
}

public String getName() {
return name;
}

public BidType getBidType() {
return bidType;
}

public BidInfo getBidInfo() {
return bidInfo;
}

public String getAccountId() {
return accountId;
}

public String getAdSetId() {
return adSetId;
}

public String getCampaignId() {
return campaignId;
}

public String getCreativeId() {
return creativeId;
}

public Targeting getTargeting() {
return targeting;
}

public Date getCreatedTime() {
return createdTime;
}

public Date getUpdatedTime() {
return updatedTime;
}

public enum AdStatus {
ACTIVE, PAUSED, CAMPAIGN_PAUSED, CAMPAIGN_GROUP_PAUSED, CREDIT_CARD_NEEDED, DISABLED, DISAPPROVED, PENDING_REVIEW,
PREAPPROVED, PENDING_BILLING_INFO, ARCHIVED, DELETED, UNKNOWN;

@JsonCreator
public static AdStatus fromValue(String value) {
for (AdStatus status : AdStatus.values()) {
if (status.name().equals(value)) {
return status;
}
}
return UNKNOWN;
}
}
}
Loading