-
-
Save dwoodard/079eca8082bcceff585c76cd025004c4 to your computer and use it in GitHub Desktop.
IG-Metrics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Carbon\Carbon; | |
use FacebookAds\Api; | |
use Illuminate\Support\Facades\Route; | |
Route::get('debug', function () { | |
Api::init(env('FB_APP_ID'), env('FB_APP_SECRET'), env('FB_ACCESS_TOKEN')); | |
$api = Api::instance(); | |
// who ever the FB_ACCESS_TOKEN belongs to | |
// $me = $api->call('/me', 'GET', [ | |
// 'fields' => 'id, accounts{instagram_business_account{id,name}}', | |
// ])->getContent(); | |
// dd($me); | |
/*Example: | |
[ | |
"instagram_business_account" => array:2 [ | |
"id" => "17841465265648246" | |
"name" => "A Come Follow Me podcast with Barbara Morgan Gardner" | |
] | |
"id" => "230947993442938" // Facebook User ID | |
] | |
*/ | |
// STEP 1: Get the Instagram Account IDs | |
// Instagram Account IDs | |
// we should store the instagram account id and the page access token in the database | |
// $accounts = $api->call('/me/accounts', 'GET', [ | |
// 'fields' => 'id,name,instagram_business_account{id}', | |
// ])->getContent(); | |
// // dd($accounts); | |
// // STEP 1.1: get and store clients instagram account id | |
// $instagram_account = collect($accounts['data'])->where('name', 'Wifi Wise')->first(); | |
// dump( | |
// $instagram_account, | |
// $instagram_account['instagram_business_account']['id'] | |
// ); | |
// $instagram_page_access_token = $api->call("/{$instagram_account['instagram_business_account']['id']}", 'GET', [ | |
// 'fields' => 'name, id', | |
// ])->getContent(); | |
// STEP 2: get the instagram account insights | |
// This gets the Instagram account insights | |
// https://developers.facebook.com/docs/instagram-platform/instagram-graph-api/reference/ig-user/insights | |
// These metrics work with the total_value metric_type and the day period (total_value/day) | |
$total_value_day = [ | |
'impressions', | |
'reach', | |
'website_clicks', | |
'profile_views', | |
'accounts_engaged', | |
'total_interactions', | |
'likes', | |
'comments', | |
'shares', | |
'saves', | |
'replies', | |
'follows_and_unfollows', | |
'profile_links_taps', | |
]; | |
/* | |
If something is categorized as non_total_value_metrics, it means | |
that the metric cannot be measured or calculated as a total value. | |
In other words, these metrics do not represent a cumulative or summed-up | |
value over a period of time. | |
For example, they may not be able to be summed up because | |
they represent a snapshot or demographic information, | |
such as follower demographics, or are not designed to | |
be aggregated like "follower count." | |
*/ | |
$non_total_value_metrics = [ | |
'follower_count', // can't do total_value | |
'email_contacts', // can't do total_value | |
'phone_call_clicks', // can't do total_value | |
'text_message_clicks', // can't do total_value | |
'get_directions_clicks', // can't do total_value | |
'online_followers', // can't do total_value | |
'views', // can't do total_value | |
'threads_likes', // can't do total_value | |
'threads_replies', // can't do total_value | |
'reposts', // can't do total_value | |
'quotes', // can't do total_value | |
'threads_followers', // can't do total_value | |
'threads_follower_demographics', // can't do total_value | |
]; | |
// These metrics can't use period: day | |
$non_day_period_metrics = [ | |
'engaged_audience_demographics', // can't use period: day | |
'reached_audience_demographics', // can't use period: day | |
'follower_demographics', // can't use period: day | |
]; | |
$instagram_metric = $api->call('/17841467021414021/insights', 'GET', [ | |
'metric' => implode(',', $total_value_day), | |
'metric_type' => 'total_value', | |
'period' => 'day', | |
'since' => Carbon::now()->subDays(30)->format('Y-m-d'), | |
'until' => Carbon::now()->format('Y-m-d'), | |
])->getContent(); | |
// STEP 2.1: format the metrics | |
$total_value_day_metrics = collect($instagram_metric['data'])->mapWithKeys(function ($item) { | |
return [$item['name'] => $item['total_value']['value']]; | |
}); | |
dd( | |
$instagram_metric, | |
$total_value_day_metrics | |
); | |
}); | |
Route::inertia('components', 'Debug/Components') | |
->name('debug.components'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment