Display Facebook ADs Statistics on webpage using Facebook Ads API in PHP - php

Does anyone know how to fetch all facebook ads statistics and display on webpage using Facebook Ads Api-PHP SDK. I am using this API and I am getting campaign details like name of campaign, id, status. but not able to get impressions,clicks, spent.
What I am doing let me share with you:
1) I am getting access token by authorizing user
2) After getting access token, I am using below code
$account = new AdAccount('act_XXXXXXXXXXXXXXX');
$account->read();
$fields = array(
AdCampaignFields::ID,
AdCampaignFields::NAME,
AdCampaignFields::OBJECTIVE,
);
$params = array(AdCampaignFields::STATUS => array(AdCampaign::STATUS_ACTIVE,AdCampaign::STATUS_PAUSED,),);
$campaigns = $account->getAdCampaigns($fields, $params);
/* Added By Jigar */
$campaign = new AdCampaign('XXXXXXXXXXXXXXXX');
$compainDetails = $campaign->read($fields);
3) then printing the array
echo "<pre>";
print_r($compainDetails);
exit;
If anyone know any suggestion in above code, please share. All code is in PHP. Dose anyone have any tutorial that fetch all above required data then share it

You could try to use the facebook insights api instead of $campaign->read. Here's an example:
https://developers.facebook.com/docs/marketing-api/insights/v2.5#create-async-jobs
What you have to do to get impressions, click and spent is to add these fields to the $fields param. In your case, the complete code should look like the following:
use FacebookAds\Object\Campaign;
use FacebookAds\Object\Values\InsightsLevels;
use FacebookAds\Object\Values\InsightsFields;
$campaign = new Campaign();
$fields = array(
InsightsFields::IMPRESSIONS,
InsightsFields::UNIQUE_CLICKS,
InsightsFields::CALL_TO_ACTION_CLICKS,
InsightsFields::INLINE_LINK_CLICKS,
InsightsFields::SOCIAL_CLICKS,
InsightsFields::UNIQUE_SOCIAL_CLICKS,
InsightsFields::SPEND,
);
$params = array(
'level' => InsightsLevels::CAMPAIGN,
);
$async_job = $campaign->getInsightsAsync($fields, $params);
$async_job->read();
I don't know what exactly the "click" param means for you, but if you take a look at all these click params, I'm sure you'll find it or you'll know how to calculate it.
For a complete list of fields available on insights objects, have a look at: https://github.com/facebook/facebook-php-ads-sdk/blob/master/src/FacebookAds/Object/Fields/InsightsFields.php
Hope that helps.
Regards, Benjamin

Related

Facebook API (PHP) : Get Full Ads List

I am using Facebook API to fetch the full Ads list.
The Code is working, But it return only 25 Ad in case of i have 150+ Ad in my account.
I guess that happens because of the query limits on the Facebook API.
My Code:
$account = new AdAccount('act_<AD_ACCOUNT_ID>');
$account->read();
$fields_adset = array(
AdSetFields::ID,
AdSetFields::NAME,
AdSetFields::CAMPAIGN_ID,
AdSetFields::STATUS,
);
$ads = $account->getAds($fields_adset);
foreach ($ads as $adset) {
$adset_id = $adset->{AdSetFields::ID};
echo $adset_id;
//print_r($adset);
//exit();
}
So, they mentioned in the documentation that :
Use Asynchronous Requests to query a huge amount of data
Reference (1) : https://developers.facebook.com/docs/marketing-api/best-practices/
Reference (2) : https://developers.facebook.com/docs/marketing-api/insights/best-practices/#asynchronous
But, I can't apply that "Asynchronous" requests to my code to fetch the Full Ad List,
Please help me to fetch the full Ads list
Thank you.
You should implement pagination (or request a limit more high). With the PHP SDK you can implement the cursor as described in the doc here or more simply set the Implicit Fetching, as example:
..
use FacebookAds\Cursor;
...
Cursor::setDefaultUseImplicitFetch(true);
$account = new AdAccount('act_<AD_ACCOUNT_ID>');
$account->read();
$fields_adset = array(
AdSetFields::ID,
AdSetFields::NAME,
AdSetFields::CAMPAIGN_ID,
AdSetFields::STATUS,
);
$ads = $account->getAds($fields_adset);
foreach ($ads as $adset) {
$adset_id = $adset->{AdSetFields::ID};
echo $adset_id;
//print_r($adset);
//exit();
}
Hope this help

Retrieve All User Lists using Aweber API

I am adding AWeber as an autoresponder in a web application. Using AWeber API, I am able to add a new subscriber to list with a known name which is in this case is firstlist:
$app = new MyApp();
$app->findSubscriber('whtever#aol.com');
$list = $app->findList('firstlist');
$subscriber = array(
'email' => 'someemail#gmail.com',
'name' => 'Name here'
);
$app->addSubscriber($subscriber, $list);
Function definition for findList() is:
function findList($listName) {
try {
$foundLists = $this->account->lists->find(array('name' => $listName));
return $foundLists[0];
}
catch(Exception $exc) {
print $exc;
}
}
As I am developing a public application, so I need to provide users an option to select from their available lists.
Please guide me how I can retrieve all the lists name.
You are returning $foundLists[0] so it will return single list. Try to return foundLists and check what it returns.
This may help you: https://labs.aweber.com/snippets/lists
In short, I pulled the lists by first finding the Aweber User Id so that I could use it in the URL https://api.aweber.com/1.0/accounts/<id>/lists
To find the User ID, I first got the account.
$this->aweber->getAccount($token['access'], $token['secret']);
Then, I retrieve the user's information.
$aweber_user = $this->aweber->loadFromUrl('https://api.aweber.com/1.0/accounts');
From that, I grabbed the user ID with...
$id = $aweber_user->data['entries'][0]['id'];
Once I had the user ID, I could then retrieve their lists with...
$lists = $this->aweber->loadFromUrl('https://api.aweber.com/1.0/accounts/'.$id.'/lists');
This example is more of a procedural approach, of course, I recommend utilizing classes.

WHMCS: How to get the current client in addon module clientarea page?

Given that I have a WHMCS addon that I call 'my_addon'. I created the main addon file 'my_addon.php' which does contain nothing than:
<?php
function my_addon_clientarea($vars) {
$client = null;
return array(
'pagetitle' => 'My Addon',
'breadcrumb' => array('index.php?m=my_addon'=>'My Addon'),
'templatefile' => 'views/myaddon_view',
'vars' => array(
'client' => $client
)
);
}
This does basically work. It does give me my template file, everything is passed through. My question is: How do I get the currently logged in client from within that function?
I didn't find any API method and I can't see any constant which does hold this information.
There must be a way to get the current client within the clientarea? Thanks for your help!
For those who do come after me and have the same problem: it's easy to solve. Turned out, that I just had to think it through... I found the client id to be available in the $_SESSION-variable.
So, if you are looking for the client's id:
<?php
function my_addon_clientarea($vars) {
$clientid = $_SESSION['uid'];
// And so on...
}
The official way to get current user information is:
$currentUser = new \WHMCS\Authentication\CurrentUser;
$user = $currentUser->user();
You can find more information here

Get users details in mixpanel api

I'm trying to get user details via Mixpanel analytics but I am having difficulties to properly grasp how i should do this… data has been set using
mixpanel.people.identify()
Events and Segmentation requests seem to be ok; but I can't figure out how to query a request on the Engage endpoint.
as it turns out—there might be other ways to find this out but this one works for me:
you need to get engagement data this way
$data = $mp->request(array('engage'));
foreach($data->results as $user) {
$email = $user->{'$properties'}->{'$email'};
$last_seen_date = $user->{'$properties'}->{'$last_seen'};
$distinct_id = $user->{'$distinct_id'};
}
the distinct_id can then be used within the foreach() loop above to get events registered with this user:
$endpoint = array('stream', 'query');
$parameters = array(
'distinct_ids' => json_encode(array($distinct_id)),
'from_date' => '2014-02-10',
'to_date' => '2014-02-25',
);

Get Facebook profile name using profile id

Am using php sdk to get facebook information. I have an array of facebook ids.
facebook ids 100003186894762, 100002039757708, 100001527538655, 100005502043347
$access_token = $facebook->getAccessToken();
$facebook ->setAccessToken($access_token);
$user_profile = $facebook->api(/100003186894762);
Here am getting one user information. Is there any way to get an array of information in a single request.
like this way
$user_profile = $facebook>api(/100003186894762/100002039757708/100001527538655/100005502043347);
I tried with looping each request but its taking too much time
$resultfbid is an array of facebook ids
foreach($resultfbid as $key=>$fbid) {
$user_profile = $facebook->api('/'.$fbid);
$posts[] = array('name'=> $user_profile['first_name'], 'id'=> $user_profile['id'],'status'=>$resultstatus[$key] );
}
Please help me to find one solution for this issue, Thanks
You can query the API for multiple IDs by using a GET parameter id and list the ids comma-separated:
https://developers.facebook.com/tools/explorer?method=GET&path=%3Ffields%3Dname%26ids%3D100003186894762%2C100002039757708%2C100001527538655%2C100005502043347
What about this?
file_get_contents( 'http://graph.facebook.com/100003186894762/picture' );

Categories