I am using the Marketing API, the Insights Edge specifically, to retrieve the data of our campaigns.
https://developers.facebook.com/docs/marketing-api/insights/v2.4
The problem is that I do not see the field for knowing how many app installs the ad has generated.
I see "clicks, impressions, reach" fields but nothing about the number of downloads from the stores.
Is there a way to know that? This is available in the Facebook Ads dashboard but I do not see any way to get that value from the API.
Thanks for any help!!
require_once(__DIR__ . "/config/config.php");
include_once(__DIR__ . "/inc/helpers.php");
require __DIR__ . '/vendor/autoload.php';
use FacebookAds\Api;
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\AdCampaign;
use FacebookAds\Object\Values\InsightsPresets;
$access_token = readFromFile(__DIR__ . "/auth/token.txt");
// Initialize a new Session and instanciate an Api object
Api::init($app_id, $app_secret, $access_token);
// The Api object is now available trough singleton
$api = Api::instance();
$account = new AdAccount('act_' . $ads_account_id);
$campaigns = $account->getAdCampaigns(array(
\FacebookAds\Object\Fields\AdCampaignFields::ID,
\FacebookAds\Object\Fields\AdCampaignFields::NAME,
)
);
foreach ($campaigns as $campaign) {
//echo $campaign->{\FacebookAds\Object\Fields\AdCampaignFields::ID}.PHP_EOL;
$campaign_insights = new AdCampaign($campaign->{\FacebookAds\Object\Fields\AdCampaignFields::ID});
$params = array(
'date_preset' => InsightsPresets::YESTERDAY,
/*
'time_range' => array(
'since' => '2015-08-03',
'until' => '2015-08-03'
),
*/
'filtering' => array(
[
'field' => 'campaign.objective',
'operator' => 'IN',
'value' => ["MOBILE_APP_INSTALLS"]
]
)
);
$insights = $campaign_insights->getInsights(array(
'reach',
'impressions',
'clicks',
'call_to_action_clicks',
'total_actions',
'spend'
), $params);
After trying every possibile field, I have found out that it's inside the "actions" field.
Thanks
Related
I am trying to update a meeting using Zoom API but I can't get it to work. I'm trying to do a PATCH request using eleague oauth2 client like this:
require '../vendor/autoload.php';
require_once '../config.php';
$id = $_POST['id'];
$topic = $_POST['topic'];
$type = $_POST['type'];
$start_time = $_POST['start_time'];
$duration = $_POST['duration'];
$agenda = $_POST['agenda'];
$params = array(
'topic' => $topic,
'type' => $type,
'start_time' => $start_time,
'duration' => $duration,
'agenda' => $agenda,
'password' => '123456'
);
$provider = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => CONEXAO_API['clientId'],
'clientSecret' => CONEXAO_API['clientSecret'],
'redirectUri' => CONEXAO_API['redirect_url'],
'urlAuthorize' => 'https://zoom.us/oauth/authorize',
'urlAccessToken' => 'https://zoom.us/oauth/token',
'urlResourceOwnerDetails' => 'https://api.zoom.us/v2/users/me'
]);
$options['body'] = json_encode( $params );
$options['headers']['Content-Type'] = 'application/json';
$options['headers']['Accept'] = 'application/json';
$request = $provider->getAuthenticatedRequest( 'PATCH',
'https://api.zoom.us/v2/meetings/'.$id,
unserialize($_SESSION['token']),
$options
);
$retorno = $provider->getParsedResponse($request);
var_dump($retorno);
I'm getting an empty response, and I'm not sure what is missing. Can anyone help me?
basically the response given by the api is blank or just meeting updated which isn't in json format. So in case you want to check whether its working go to zoom meeting panel and open the meeting which you are updating using api and run the api and check the meeting is updated in zoom meeting panel.
The problem is that the method getParsedResponse for some reason is not returning any message. But the script is working, it's updating the meeting the way it supposed to.
If I get the response using the method getResponse I can see the statusCode 204, which means that everything went ok.
I'm using ZF2 Twitter package to get user info by username and retrieve it's location, but the returned object doesn't give me the "derived" property, that should have the detailed data about location I want to use.
public function twitterAction()
{
$config = array(
'access_token' => array(
'token' => 'MY TOKEN',
'secret' => 'MY SECRET',
),
'oauth_options' => array(
'consumerKey' => 'MY CONSUMER KEY',
'consumerSecret' => 'MY COMSUMER SECRET',
),
'http_client_options' => array(
'adapter' => 'Zend\Http\Client\Adapter\Curl',
'curloptions' => array(
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
),
),
);
$twitter = new Twitter($config);
$response = $twitter->account->accountVerifyCredentials();
if (!$response->isSuccess()) {
die(var_dump($response->getErrors()));
}
$params = $this->params()->fromRoute();
$profile = $params['profile'];
$user = $twitter->users->show($profile);
$coordinates = $user->derived->geo->coordinates;
return new JsonModel(
[
'placeName' => $user->derived->locality . ' - ' $user->derived->region,
'link' => 'http://www.google.com/maps/place/'.$coordinates[0].','.$coordinates[1]
]
);
}
I'm using the twitter api page as reference
this is the response I get if I return the user
{
'placename: ': '-',
'link: ': 'http://www.google.com/maps/place/,',
}
How can I retrieve this data?
From that page:
Profile Geo data will be included in Twitter's PowerTrack, Replay, Volume Stream, Search, and Historical PowerTrack APIs.
I think that means the geo data is only available for paying customers of PowerTrack. If you're just a regular developer, you can't get it.
I got some example scripts from Facebook App Management to use the Marketing API. When I run the script, I just get this error by curl:
'Unsupported post request. Object with ID \'105101623679981\' does not exist, cannot be loaded due to missing permissions, or does not support this operation.
I already tried to deactivate the Sandbox Mode and go public, tried many different scripts in different languages and also other keys.
Any Ideas?
This is the Script:
<?php
//Add all those Uses and the autoloader
$access_token = '<my_very_long_accessToken';
$ad_account_id = '<my_account_id>'; //<-- This is the Object in the Error Code
$app_secret = '<my_app_secret>';
$page_id = '<my_page_id>';
$app_id = '<my_app_is>';
$api = Api::init($app_id, $app_secret, $access_token);
$api->setLogger(new CurlLogger());
$fields = array(
);
$params = array(
'objective' => 'PAGE_LIKES',
'status' => 'PAUSED',
'buying_type' => 'AUCTION',
'name' => 'My Campaign',
);
$campaign = (new AdAccount($ad_account_id))->createCampaign(
$fields,
$params
);
$campaign_id = $campaign->id;
echo 'campaign_id: ' . $campaign_id . "\n\n";
$fields = array(
);
$params = array(
'status' => 'PAUSED',
'targeting' => array('geo_locations' => array('countries' => array('US'))),
'daily_budget' => '1000',
'billing_event' => 'IMPRESSIONS',
'bid_amount' => '20',
'campaign_id' => $campaign_id,
'optimization_goal' => 'PAGE_LIKES',
'promoted_object' => array('page_id' => $page_id),
'name' => 'My AdSet',
);
//...
Yeah. I got it. It has to be "act_"
So bad. The Script is really crappy. So many Errors. And it's created by facebook!
I am getting error while creating Creative using the FB Ads PHP SDK
$parent_id as a parameter of constructor is being deprecated, please try not to use this in new code.
The code was working before the 2.9 and 2.10 update.
The Code I am using to create Creative is:
$link_data = new AdCreativeLinkData();
$link_data->setData(array(
AdCreativeLinkDataFields::MESSAGE => 'Product Description',
AdCreativeLinkDataFields::LINK => $url_of_website,
AdCreativeLinkDataFields::IMAGE_HASH => $image->hash,
AdCreativeLinkDataFields::DESCRIPTION => 'Link Description',
AdCreativeLinkDataFields::CALL_TO_ACTION => array(
'type' => AdCreativeCallToActionTypeValues::LEARN_MORE,
'value' => array(
'link_title' => 'View Similar Products Now!',
'lead_gen_form_id' => $form_id,
),
),
));
$story = new AdCreativeObjectStorySpec();
$story->setData(array(
AdCreativeObjectStorySpecFields::PAGE_ID => $page_id,
AdCreativeObjectStorySpecFields::LINK_DATA => $link_data,
));
$creative = new AdCreative(null, $account_id);
$creative->setData(array(
AdCreativeFields::NAME => $nm,
AdCreativeFields::OBJECT_STORY_SPEC => $story,
AdCreativeFields::URL_TAGS => 'product=' . $p_id,
));
$creative->create();
I do not see any parent id in this statement. Please help
$parent_id is deprecated
The issue was reported on facebook github with issue# 314
Response from Facebook Developer
"We are depreciating creation with parent_id. We are seeing multiple endpoints that can create the same type of object. We do not have good ways to decide which one we should use if you are creating new object with parent_id. Moving forward, please instantiate the parent object with the parent_id and call create_XXX function to create the object you want."
Sample Code:
use FacebookAds\Object\AdCreative;
use FacebookAds\Object\AdCreativeLinkData;
use FacebookAds\Object\Fields\AdCreativeLinkDataFields;
use FacebookAds\Object\AdCreativeObjectStorySpec;
use FacebookAds\Object\Fields\AdCreativeObjectStorySpecFields;
use FacebookAds\Object\Fields\AdCreativeFields;
$link_data = new AdCreativeLinkData();
$link_data->setData(array(
AdCreativeLinkDataFields::MESSAGE => 'try it out',
AdCreativeLinkDataFields::LINK => '<URL>',
AdCreativeLinkDataFields::IMAGE_HASH => '<IMAGE_HASH>',
));
$object_story_spec = new AdCreativeObjectStorySpec();
$object_story_spec->setData(array(
AdCreativeObjectStorySpecFields::PAGE_ID => <PAGE_ID>,
AdCreativeObjectStorySpecFields::LINK_DATA => $link_data,
));
$creative = new AdCreative(null, 'act_<AD_ACCOUNT_ID>');
$creative->setData(array(
AdCreativeFields::NAME => 'Sample Creative',
AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
));
$creative->create();
Hope this helps.
Use setParentId($parrent_id).
Sample code:
$product_catalog = new ProductCatalog();
$product_catalog->setParentId($parrent_id);
$product_catalog->setData(
[
ProductCatalogFields::NAME => "Testjon Autojon Catalog",
ProductCatalogFields::VERTICAL => "vehicles",
]
);
$product_catalog->create();
I found even though the accepted answer mentioned in here, which is the use of $parent_id is deprecated, the sample code shared there is still shows the old way of doing it.
In that example, the second argument passed to in AdCreative() is still the $parent_id.
$creative = new AdCreative(null, 'act_<AD_ACCOUNT_ID>');
For clarity mentioned below is the method signature of the constructor of \FacebookAds\Object\AbstractCrudObject which is what \FacebookAds\Object\AdCreative is extended from and, you'd see the deprecation notice there.
/**
* #deprecated deprecate constructor with null and parent_id
* #param string $id Optional (do not set for new objects)
* #param string $parent_id Optional, needed for creating new objects.
* #param Api $api The Api instance this object should use to make calls
*/
public function __construct($id = null, $parent_id = null, Api $api = null) {
parent::__construct();
//...
}
Said that as for the new approach, this is the way it should be done now :)
require __DIR__ . '/vendor/autoload.php';
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
use FacebookAds\Object\AdAccount;
$access_token = '<ACCESS_TOKEN>';
$app_secret = '<APP_SECRET>';
$app_id = '<APP_ID>';
$id = '<AD_ACCOUNT_ID>';
$api = Api::init($app_id, $app_secret, $access_token);
$api->setLogger(new CurlLogger());
$fields = array();
$params = array(
'name' => 'Sample Creative',
'object_story_spec' => ['page_id' => '<pageID>',
'video_data' => ['IMAGE_URL' => '<imageURL>',
'video_id' => '<videoID>',
'call_to_action' => ['type' => 'LIKE_PAGE',
'value' => ['page' => '<pageID>']
]
]
],
);
$adCreative = (new AdAccount($id))->createAdCreative($fields, $params);
echo json_encode($adCreative->exportAllData(), JSON_PRETTY_PRINT);
I found this example here. Please note even though the title of this document is "Create an Ad Video Creative" it actually shows how to create the Ad creative. There are numerous inconsistencies in the Facebook Marketing API reference and this is such a case :)
I'm attempting to just get the spend for specific Ad Accounts from the previous day. Whenever I send up the request:
$api = Api::instance();
$account = new AdAccount('act_<account_id>');
$params = array(
'level' => 'account',
'date_preset' => 'yesterday',
'fields' => ['spend', 'account_id'],
);
$insights = $account->getInsights(array(), $params);
print_r($insights);
I get a large protected object back and can't seem to find any documentation as to how to access the actual requested data.
//Get HLG Ad Spend
$account = new AdAccount('act_10153795481711178');
$params = array(
'level' => 'account',
'date_preset' => 'yesterday',
'fields' => ['spend', 'account_id']
);
$insights = $account->getInsights(array(), $params);
$hlgData = $insights->getResponse()->getContent()['data'];
if(isset($hlgData[0]))
{
$hlgSpendFB = $hlgData[0]['spend'];
}else{
$hlgSpendFB = "0";
}
Above is how I was able to get the data from the Ad Account for spend from previous day. After digging through the object itself I was able to find the getter functions that procured the data requested.