facebook / facebook-ruby-business-sdk

Ruby SDK for Meta Marketing API
https://developers.facebook.com/docs/business-sdk
Other
208 stars 161 forks source link

Gem is making more calls than necessary #52

Closed kurko closed 2 years ago

kurko commented 6 years ago

tl;dr there are 2 problems, Batch doesn't return a response with anything other than ids and the gem makes N requests for N attributes that are called on an object.

Context

I'm creating many AdSets with FacebookAds::Batch.with_batch. When I do batch.execute, I get back only ids:

[
  #<FacebookAds::AdSet {:id=>\"some-id-1\"}>,
  #<FacebookAds::AdSet {:id=>\"some-id-2\"}>,
  #<FacebookAds::AdSet {:id=>\"some-id-3\"}>
]

I need to access its name and status (I want to double check that those were created correctly). When I do object.first.name, it makes Request1:

Request GET https://graph.facebook.com/v3.0/some-id-1?access_token=my-access-token&appsecret_proof=secret-prood&fields=name

Calling object.first.status, another request is made:

Request GET https://graph.facebook.com/v3.0/some-id-1?access_token=my-access-token&appsecret_proof=secret-prood&fields=status

The diff is the fields at the end. That led me to just load those ids with ::FacebookAds::AdSet

The Problem

I want to load those records, so I do the following for each id:

::FacebookAds::AdSet.get('some-id-1', 'name,status,campaign_id', session)

That works, except that in reality I'm creating 3,000 ad sets for N customers. Facebook simply won't accept that many requests in serial. I tried using FacebookAds::Batch.with_batch to no avail.

Questions

  1. Is there a way to create records in a batch and get them back with all their attributes, not just id?
  2. Is there a way to call object.name and have it request N attributes at once, instead of one call per attribute? This is very inconvenient.
  3. Is there a way to load N adsets by ids without making N requests?

Thanks a lot.

euisan commented 6 years ago

Ad Set endpoint support Read-After-Write, so you can use it.

FacebookAds::Batch.with_batch do
  5.times.map do |n|
    ad_account.adsets.create(..., fields: %w[id name status])
  end
end.execute

=> [#<FacebookAds::AdSet {:id=>"...", :name=>"...", :status=>"..."}>,
 #<FacebookAds::AdSet {:id=>"...", :name=>"...", :status=>"..."}>,
 #<FacebookAds::AdSet {:id=>"...", :name=>"...", :status=>"..."}>,
 #<FacebookAds::AdSet {:id=>"...", :name=>"...", :status=>"..."}>,
 #<FacebookAds::AdSet {:id=>"...", :name=>"...", :status=>"..."}>]

It's little bit hacky, but you also possible using Multiple ID Read Requests with this.

ids = [<ADSET_ID>, <ADSET_ID>, ...]

FacebookAds::APIRequest.new(
  :get,
  '',
  params: { ids: ids.join(","), fields: "id,name,status" },
  session: <SESSION>
).execute_now

=> #<FacebookAds::APIResponse:0x00007fd62c2fa008
 @body=<BODY>,
 @headers=<HEADERS>,
 @status_code=<HTTP_STATUS>>
stale[bot] commented 4 years ago

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

kurko commented 4 years ago

Still waiting for some official answer

stale[bot] commented 4 years ago

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

kurko commented 4 years ago

Still waiting for some official answer

stale[bot] commented 4 years ago

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

kurko commented 4 years ago

Still waiting for some official answer

stale[bot] commented 3 years ago

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 2 years ago

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.