Get users details in mixpanel api - php

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',
);

Related

Active Collab API: How to get projects

I'm trying out the ActiveCollab API for my first time. I had to use StackOveflow to figure out how to get the API token since the docs don't tell me this.
Below is my code:
/* GET INTENT */
$url = 'https://my.activecollab.com/api/v1/external/login';
$fields = array(
'email' => "email#email.com",
'password' => "****"
);
$intent = curl_post_connector($url, $fields);
$intent = $intent->user->intent;
/* GET TOKEN */
$url = 'https://app.activecollab.com/my_app_id/api/v1/issue-token-intent';
$fields = array(
'intent' => $intent,
'client_name' => 'My App Name',
'client_vendor' => 'My Company Name'
);
$token = curl_post_connector($url, $fields);
$token = $token->token;
Everything above works and get's the token properly. What I find really weird is that I have to use API v1 to get this, and the docs on ActiveCollab's site don't mention any URL for API v5. It seems like this is the approach everything is taking here on StackOverflow.
Now with the token, I try to get my list of projects:
/* GET PROJECT */
$url = 'https://app.activecollab.com/my_app_id/api/v1/users';
$headers = array (
"X-Angie-AuthApiToken" => $token
);
$projects = curl_get_connector($url, $headers);
var_dump($projects);
But this does not work. There is no error returned - it instead returns an array of languages for some reason! I don't want to paste the massive json object here, so instead I'll link you to a photo of it: https://www.screencast.com/t/7p5JuFB4Gu
UPDATE:
When attempting to use the SDK, it works up until I try getting the token (which is just as far as I got without the SDK). I'm getting Server Error 500, and when looking at the logs, it says:
/home/working/public_html/ac/index.php(21): ActiveCollab\SDK\Authenticator\Cloud->issueToken(123456789)
#1 {main}
thrown in /home/working/public_html/ac/SDK/Authenticator/Cloud.php on line 115
This is line 115 of Cloud.php:
throw new InvalidArgumentException("Account #{$account_id} not loaded");
I honestly don't think I did anything wrong... there must be something wrong with my account ID.
Just for kicks, I commented out that line, and the error disappears and the page loads fine - except now I have no token...

API pagination, how to implement page token paging method?

I am working on implementing an API interface for my project.
As i know, there are different forms to make pagination through the results, like the following:
https://example.com/api/purchaseorders?page=2&pagesize=25
But, i see many APIs like google use a different approach, in which they use a "pageToken" to let the user move between the pages of results, for example:
https://example.com/api/purchaseorders?pagesize=25&pageToken=ClkKHgoRc291cmNlX2NyZWF0ZWRfYXQSCQjA67Si5sr
So instead of page=2 they used pageToken=[token].
It is not clear for me the idea of pageToken and how to implement it.
It will be helpful if you guide me to any resources so i can get more knowledge.
Thank you.
Here's a very simple standalone example using the filesystem as a keyvalue store (since a filesystem will always be available).
$requestParameters = [];
if (($token = filter_input(INPUT_GET,"pageToken")) && is_readable("/tmp/$token")) {
$requestParameters = file_get_contents("/tmp/$token");
} else {
$requestParameters = [
"q" => filter_input(INPUT_GET,"q"),
"pageSize" => filter_input(INPUT_GET,"pageSize",FILTER_VALIDATE_INT),
"page" => filter_input(INPUT_GET,"page",FILTER_VALIDATE_INT)
];
}
$nextPageRequestParameters = $requestParameters;
$nextPageRequestParameters["page"]++;
$nextPageToken = md5(serialize($nextPageRequestParameters)); //This is not ideal but at least people can't guess it easily.
file_put_contents("/tmp/$nextPageToken", serialize($nextPageRequestParameters));
//Do request using $requestParameters
$result = [ "nextPageToken" => $nextPageToken, "data" => $resultData ];
echo json_encode($result);

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

Facebook graph PHP API batch request

I am trying to get to get clicks and spend data for ad campaigns.
I am currently getting all ad campaign ID's with a curl request which returns about 260 ID's.
I want to make a batch request and get the clicks, spend, start and end dates for each ID.
I have found the PHP SDK FacebookRequest() function very confusing so have been trying to make cURL requests.
Would really appreciate some help because I am just stumped at the moment. Is it best to use to FacebookRequest() function or can I continue using the cURL requests?
Not sure if I am on the right track but essentially what I have at the moment is all the campaign ID's which I group with a method, relative_url and body and then pass to a requestHandler function. The code is as follows:
$ad_account_ids = <ad_account_id>;
$ad_campaign_ids = FbAdCampaign::all()->lists('ad_campaign_id')->toArray();
foreach ($ad_campaign_ids as $key => $value) {
$ad_campaign_ids[$key] = array(
"method" => "GET",
"relative_url" => "v2.4/act_".$ad_account_ids."/adgroups",
"body" => "campaign_id=".$value."&redownload=1&bid_type=CPC&bid_info={\"clicks\":150}&creative={\"creative_id\":\"{result=create_creative:$.id}\"}&targeting={\"countries\":[\"US\"]}&name=test1"
);
$fields[] = array(
'access_token' => $access_token,
'batch' => $ad_campaign_ids[$key]
);
// $url = 'https://graph.facebook.com/v2.4/act_'.$ad_campaign_ids.'/adcampaign_groups?access_token='.$access_token;
$url = 'https://graph.facebook.com/';
$data = RequestHandler::curlRequest($url);
Ok got it.
Had to prepend the /GET data to the end of the URL to pass to the request handler.
as follows:
$url = 'https://graph.facebook.com/v2.3/act_'.$ad_account_ids.'/adcampaign_groups?access_token='.$access_token;

Display Facebook ADs Statistics on webpage using Facebook Ads API in 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

Categories