├── .gitignore ├── .travis.yml ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Jonasva │ └── FacebookInsights │ │ ├── Facades │ │ └── FacebookInsights.php │ │ ├── FacebookInsights.php │ │ └── FacebookInsightsServiceProvider.php └── config │ ├── .gitkeep │ └── config.php └── tests └── .gitkeep /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | .idea -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | 6 | before_script: 7 | - curl -s http://getcomposer.org/installer | php 8 | - php composer.phar install --dev 9 | 10 | script: phpunit -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Facebook Insights 2 | 3 | FacebookInsights provides a quick way to access insights of a facebook page with the Facebook OpenGraph API v2. It works with a permanent access token so no user interaction is required. A common usage would be to have a statistics dashboard that needs to regularly fetch insights of a facebook page. 4 | 5 | ## Installation 6 | 7 | To get the latest version of FacebookInsights require it in your `composer.json` file. 8 | 9 | ~~~ 10 | "jonasva/laravel-facebook-insights": "dev-master" 11 | ~~~ 12 | 13 | *(For Laravel 4, please check the documentation of the Laravel4 branch of this repository)* 14 | 15 | Run `composer update jonasva/laravel-facebook-insights` to install it. 16 | 17 | Once FacebookInsights is installed you need to register its service provider with your application. Open `config/app.php` and find the `providers` key. 18 | 19 | ~~~php 20 | 'providers' => array( 21 | 22 | Jonasva\FacebookInsights\FacebookInsightsServiceProvider::class, 23 | 24 | ) 25 | ~~~ 26 | 27 | A Facade for easy access is also included. You can register the facade in the `aliases` key of your `config/app.php` file. 28 | 29 | ~~~php 30 | 'aliases' => array( 31 | 32 | 'FacebookInsights' => Jonasva\FacebookInsights\Facades\FacebookInsights::class, 33 | 34 | ) 35 | ~~~ 36 | 37 | ### Publish the configurations 38 | 39 | Run this on the command line from the root of your project: 40 | 41 | ~~~ 42 | $ php artisan vendor:publish 43 | ~~~ 44 | 45 | A configuration file will be published to `config/facebook-insights.php` 46 | 47 | ### Config 48 | 49 | #### Facebook App and Page information 50 | 51 | To use this package, you'll need to setup your Facebook App ID, App secret, (permanent) access token and Page ID. For more information about this check the config file. 52 | 53 | #### Cache 54 | 55 | Facebook GraphApi responses get cache for 1 day by default. You can change this by altering the `cache-lifetime`. 56 | 57 | ## Usage 58 | 59 | The package contains several useful methods to fetch facebook insights with the OpenGraph API. Methods can be called by using the facade `FacebookInsights`. 60 | For example: 61 | ```php 62 | use FacebookInsights; // this goes above your class declaration 63 | 64 | // ... 65 | 66 | $startDate = new \DateTime('2015-03-15'); 67 | $endDate = new \DateTime('2015-03-25'); 68 | // fetch your page's total impressions for a given period 69 | $totalImpressions = FacebookInsights::getPageTotalImpressions($startDate, $endDate); 70 | ``` 71 | 72 | ## Methods 73 | 74 | This package currently provides insights for Page and Post objects. That said, any other OpenGraph queries can also be done by simply using the following method: 75 | ```php 76 | /** 77 | * Construct a facebook request 78 | * 79 | * @param string $query 80 | * @param array $params (optional) 81 | * @param string $method (optional) 82 | * @param string $object (optional) 83 | * 84 | * @return GraphObject 85 | */ 86 | public function performGraphCall($query, $params = [], $object = null, $method = 'GET') 87 | ``` 88 | 89 | ### Page Insights 90 | 91 | Get the total amount of page fans (aka followers, people who liked the page) 92 | ```php 93 | /** 94 | * Get the total amount of page fans (people who liked the page) 95 | * 96 | * @return int 97 | */ 98 | public function getPageTotalFans() 99 | ``` 100 | 101 | Get new fans per day for a given period 102 | ```php 103 | /** 104 | * Get new page fans per day for a given period 105 | * 106 | * @param \DateTime $startDate 107 | * @param \DateTime $endDate 108 | * 109 | * @return array 110 | */ 111 | public function getPageNewFansPerDay(\DateTime $startDate, \DateTime $endDate) 112 | ``` 113 | 114 | Get the total number of new page fans for a given period 115 | ```php 116 | /** 117 | * Get the total number of new page fans for a given period 118 | * 119 | * @param \DateTime $startDate 120 | * @param \DateTime $endDate 121 | * 122 | * @return int 123 | */ 124 | public function getPageTotalNewFans(\DateTime $startDate, \DateTime $endDate) 125 | ``` 126 | 127 | Get a page's impressions (The total number of impressions seen of any content associated with your Page) per day for a given period 128 | ```php 129 | /** 130 | * Get the page impressions per day for a given period 131 | * 132 | * @param \DateTime $startDate 133 | * @param \DateTime $endDate 134 | * 135 | * @return array 136 | */ 137 | public function getPageImpressionsPerDay(\DateTime $startDate, \DateTime $endDate) 138 | ``` 139 | 140 | Get the total number of page impressions for a given period 141 | ```php 142 | /** 143 | * Get the total number of page impressions for a given period 144 | * 145 | * @param \DateTime $startDate 146 | * @param \DateTime $endDate 147 | * 148 | * @return int 149 | */ 150 | public function getPageTotalImpressions(\DateTime $startDate, \DateTime $endDate) 151 | ``` 152 | 153 | Get a page's consumptions (The number of times people clicked on any of your content) per day for a given period 154 | ```php 155 | /** 156 | * Get the page consumptions per day for a given period 157 | * 158 | * @param \DateTime $startDate 159 | * @param \DateTime $endDate 160 | * 161 | * @return array 162 | */ 163 | public function getPageConsumptionsPerDay(\DateTime $startDate, \DateTime $endDate) 164 | { 165 | ``` 166 | 167 | Get the total number of page consumptions for a given period 168 | ```php 169 | /** 170 | * Get the total number of page consumptions for a given period 171 | * 172 | * @param \DateTime $startDate 173 | * @param \DateTime $endDate 174 | * 175 | * @return int 176 | */ 177 | public function getPageTotalConsumptions(\DateTime $startDate, \DateTime $endDate) 178 | ``` 179 | 180 | Get like, comment, share, rsvp, claim and answer counts for a page's posts grouped per day for a given period 181 | ```php 182 | /** 183 | * Get a page's positive feedback per day for a given period 184 | * The following actions are categorized as positive feedback: 185 | * like, comment, link (share), rsvp (respond to an event), claim, answer 186 | * 187 | * @param \DateTime $startDate 188 | * @param \DateTime $endDate 189 | * 190 | * @return array 191 | */ 192 | public function getPagePositiveFeedbackPerDay(\DateTime $startDate, \DateTime $endDate) 193 | ``` 194 | 195 | Get accumulated (total) like, comment, share, rsvp, claim and answer counts for a page's posts grouped per day for a given period 196 | ```php 197 | /** 198 | * Get a page's accumulated positive feedback for a given period 199 | * The following actions are categorized as positive feedback: 200 | * like, comment, link (share), rsvp (respond to an event), claim, answer 201 | * 202 | * @param \DateTime $startDate 203 | * @param \DateTime $endDate 204 | * 205 | * @return array 206 | */ 207 | public function getPageTotalPositiveFeedback(\DateTime $startDate, \DateTime $endDate) 208 | ``` 209 | 210 | Get a specific insight for a page for a given period. Insights can be found here: https://developers.facebook.com/docs/graph-api/reference/v2.2/insights#page_impressions 211 | ```php 212 | /** 213 | * Get a specific insight for a page for a given period 214 | * 215 | * @param \DateTime $startDate 216 | * @param \DateTime $endDate 217 | * @param string $insight 218 | * @param string $period (optional) 219 | * 220 | * @return int 221 | */ 222 | public function getPageInsight(\DateTime $startDate, \DateTime $endDate, $insight, $period = 'day') 223 | ``` 224 | 225 | Get a page's posts for a given period. This is not really an insight, but is needed to get post ID's which can later be used to collect post insights. 226 | ```php 227 | /** 228 | * Get the page's posts for a given period 229 | * 230 | * @param \DateTime $startDate 231 | * @param \DateTime $endDate 232 | * @param int $limit 233 | * 234 | * @return array 235 | */ 236 | public function getPagePosts(\DateTime $startDate, \DateTime $endDate, $limit = null) 237 | ``` 238 | 239 | ### Switching to another page 240 | 241 | It is also possible to dynamically switch to another page, to fetch its insights / posts. You can use the `switchPage` method for that: 242 | 243 | ```php 244 | /* 245 | * Switch to another page to get insights of 246 | * 247 | * @param string $pageId 248 | * @param string $accessToken 249 | */ 250 | public function switchPage($pageId, $accessToken) 251 | ``` 252 | 253 | Example: 254 | ```php 255 | FacebookInsights::switchPage('other page id', 'other page's permanent access token'); 256 | ``` 257 | 258 | ### Post Insights 259 | 260 | Post specific insights can only be collected by period `lifetime`, so no date range needs to be given. 261 | 262 | Get a post's impressions 263 | ```php 264 | /** 265 | * Get a post's impressions 266 | * 267 | * @param string $postId 268 | * 269 | * @return int 270 | */ 271 | public function getPostImpressions($postId) 272 | ``` 273 | 274 | Get a post's consumptions 275 | ```php 276 | /** 277 | * Get a post's consumptions 278 | * 279 | * @param string $postId 280 | * 281 | * @return int 282 | */ 283 | public function getPostConsumptions($postId) 284 | ``` 285 | 286 | Get a specific insight for a post. Post insights can be found here: https://developers.facebook.com/docs/graph-api/reference/v2.2/insights#post_impressions 287 | ```php 288 | /** 289 | * Get a specific insight for a post 290 | * 291 | * @param string $insight 292 | * @param string $postId 293 | * 294 | * @return array 295 | */ 296 | public function getPostInsight($postId, $insight) 297 | ``` 298 | 299 | Get the page's posts with calculated insights for a given period 300 | ```php 301 | /** 302 | * Get the page's posts with calculated insights for a given period 303 | * 304 | * @param \DateTime $startDate 305 | * @param \DateTime $endDate 306 | * @param int $limit 307 | * 308 | * @return array 309 | */ 310 | public function getPagePostsBasicInsights(\DateTime $startDate, \DateTime $endDate, $limit = null) 311 | ``` -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jonasva/laravel-facebook-insights", 3 | "description": "Facebook page insights integration with Laravel", 4 | "homepage": "http://github.com/jonasva/laravel-facebook-insights", 5 | "keywords": [ 6 | "facebook", 7 | "insights", 8 | "laravel" 9 | ], 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Jonas Van Assche", 14 | "email": "jonas.vanassche@gmail.com" 15 | } 16 | ], 17 | "require": { 18 | "php": ">=5.4.0", 19 | "illuminate/support": "~5.0", 20 | "facebook/php-sdk-v4": "4.0.*" 21 | }, 22 | "autoload": { 23 | "psr-0": { 24 | "Jonasva\\FacebookInsights": "src/" 25 | } 26 | }, 27 | "prefer-stable": true, 28 | "minimum-stability": "dev" 29 | } -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/Jonasva/FacebookInsights/Facades/FacebookInsights.php: -------------------------------------------------------------------------------- 1 | config = $config; 49 | $this->pageId = $this->config->get('facebook-insights.page-id'); 50 | 51 | FacebookSession::setDefaultApplication($this->config->get('facebook-insights.app-id'), $this->config->get('facebook-insights.app-secret')); 52 | 53 | $this->session[$this->pageId] = new FacebookSession($this->config->get('facebook-insights.access-token')); 54 | } 55 | 56 | /* 57 | * Switch to another page to get insights of 58 | * 59 | * @param string $pageId 60 | * @param string $accessToken 61 | */ 62 | public function switchPage($pageId, $accessToken) 63 | { 64 | $this->pageId = $pageId; 65 | 66 | if (!isset($this->session[$this->pageId])) { 67 | $this->session[$this->pageId] = new FacebookSession($accessToken); 68 | } 69 | } 70 | 71 | /** 72 | * Get the total amount of page fans (people who liked the page) 73 | * 74 | * @return int 75 | */ 76 | public function getPageTotalFans() 77 | { 78 | return array_pop($this->performGraphCall('/insights/page_fans')->getProperty('data')->asArray()[0]->values)->value; 79 | } 80 | 81 | /** 82 | * Get new page fans per day for a given period 83 | * 84 | * @param \DateTime $startDate 85 | * @param \DateTime $endDate 86 | * 87 | * @return array 88 | */ 89 | public function getPageNewFansPerDay(\DateTime $startDate, \DateTime $endDate) 90 | { 91 | $params = ['period' => 'day']; 92 | 93 | return $this->getDataForDateRange($startDate, $endDate, '/insights/page_fan_adds', $params); 94 | } 95 | 96 | /** 97 | * Get the total number of new page fans for a given period 98 | * 99 | * @param \DateTime $startDate 100 | * @param \DateTime $endDate 101 | * 102 | * @return int 103 | */ 104 | public function getPageTotalNewFans(\DateTime $startDate, \DateTime $endDate) 105 | { 106 | $rawData = $this->getPageNewFansPerDay($startDate, $endDate); 107 | 108 | return $this->calculateTotal($rawData); 109 | } 110 | 111 | /** 112 | * Get the page impressions per day for a given period 113 | * 114 | * @param \DateTime $startDate 115 | * @param \DateTime $endDate 116 | * 117 | * @return array 118 | */ 119 | public function getPageImpressionsPerDay(\DateTime $startDate, \DateTime $endDate) 120 | { 121 | $params = ['period' => 'day']; 122 | 123 | return $this->getDataForDateRange($startDate, $endDate, '/insights/page_impressions', $params); 124 | } 125 | 126 | /** 127 | * Get the total number of page impressions for a given period 128 | * 129 | * @param \DateTime $startDate 130 | * @param \DateTime $endDate 131 | * 132 | * @return int 133 | */ 134 | public function getPageTotalImpressions(\DateTime $startDate, \DateTime $endDate) 135 | { 136 | $rawData = $this->getPageImpressionsPerDay($startDate, $endDate); 137 | 138 | return $this->calculateTotal($rawData); 139 | } 140 | 141 | /** 142 | * Get the page consumptions per day for a given period 143 | * 144 | * @param \DateTime $startDate 145 | * @param \DateTime $endDate 146 | * 147 | * @return array 148 | */ 149 | public function getPageConsumptionsPerDay(\DateTime $startDate, \DateTime $endDate) 150 | { 151 | $params = ['period' => 'day']; 152 | 153 | return $this->getDataForDateRange($startDate, $endDate, '/insights/page_consumptions', $params); 154 | } 155 | 156 | /** 157 | * Get the total number of page consumptions for a given period 158 | * 159 | * @param \DateTime $startDate 160 | * @param \DateTime $endDate 161 | * 162 | * @return int 163 | */ 164 | public function getPageTotalConsumptions(\DateTime $startDate, \DateTime $endDate) 165 | { 166 | $rawData = $this->getPageConsumptionsPerDay($startDate, $endDate); 167 | 168 | return $this->calculateTotal($rawData); 169 | } 170 | 171 | /** 172 | * Get a page's positive feedback per day for a given period 173 | * The following actions are categorized as positive feedback: 174 | * like, comment, link (share), rsvp (respond to an event), claim, answer 175 | * 176 | * @param \DateTime $startDate 177 | * @param \DateTime $endDate 178 | * 179 | * @return array 180 | */ 181 | public function getPagePositiveFeedbackPerDay(\DateTime $startDate, \DateTime $endDate) 182 | { 183 | $params = ['period' => 'day']; 184 | 185 | return $this->getDataForDateRange($startDate, $endDate, '/insights/page_positive_feedback_by_type', $params); 186 | } 187 | 188 | /** 189 | * Get a page's accumulated positive feedback for a given period 190 | * The following actions are categorized as positive feedback: 191 | * like, comment, link (share), rsvp (respond to an event), claim, answer 192 | * 193 | * @param \DateTime $startDate 194 | * @param \DateTime $endDate 195 | * 196 | * @return array 197 | */ 198 | public function getPageTotalPositiveFeedback(\DateTime $startDate, \DateTime $endDate) 199 | { 200 | $rawData = $this->getPagePositiveFeedbackPerDay($startDate, $endDate); 201 | 202 | $processedResult['likes'] = $processedResult['shares'] = $processedResult['comments'] = $processedResult['rsvps'] = $processedResult['claims'] = $processedResult['answers'] = 0; 203 | 204 | foreach ($rawData as $feedback) { 205 | $processedResult['likes'] += $feedback->value->like; 206 | $processedResult['shares'] += $feedback->value->link; 207 | $processedResult['comments'] += $feedback->value->comment; 208 | $processedResult['rsvps'] += $feedback->value->rsvp; 209 | $processedResult['claims'] += $feedback->value->claim; 210 | $processedResult['answers'] += $feedback->value->answer; 211 | } 212 | 213 | return $processedResult; 214 | } 215 | 216 | /** 217 | * Get a specific insight for a page for a given period 218 | * 219 | * @param \DateTime $startDate 220 | * @param \DateTime $endDate 221 | * @param string $insight 222 | * @param string $period (optional) 223 | * 224 | * @return array 225 | */ 226 | public function getPageInsight(\DateTime $startDate, \DateTime $endDate, $insight, $period = 'day') 227 | { 228 | $params = ['period' => $period]; 229 | 230 | return $this->getDataForDateRange($startDate, $endDate, '/insights/' . $insight, $params, null, false); 231 | } 232 | 233 | /** 234 | * Get the page's posts for a given period 235 | * 236 | * @param \DateTime $startDate 237 | * @param \DateTime $endDate 238 | * @param int $limit 239 | * 240 | * @return array 241 | */ 242 | public function getPagePosts(\DateTime $startDate, \DateTime $endDate, $limit = null) 243 | { 244 | $params = $limit ? ['limit' => $limit] : []; 245 | 246 | return $this->getDataForDateRange($startDate, $endDate, '/posts', $params, null, false); 247 | } 248 | 249 | /** 250 | * Get the page's posts with calculated insights for a given period 251 | * 252 | * @param \DateTime $startDate 253 | * @param \DateTime $endDate 254 | * @param int $limit 255 | * 256 | * @return array 257 | */ 258 | public function getPagePostsBasicInsights(\DateTime $startDate, \DateTime $endDate, $limit = null) 259 | { 260 | $posts = $this->getPagePosts($startDate, $endDate, $limit); 261 | 262 | $processedResult = []; 263 | 264 | foreach($posts as $post) { 265 | $insight = $this->getPostInsight($post->id, 'post_story_adds_by_action_type'); 266 | 267 | $processedResult[$post->id]['message'] = isset($post->message) ? $post->message : $post->story; 268 | $processedResult[$post->id]['created_time'] = $post->created_time; 269 | 270 | if (!empty($insight)) { 271 | $insight = $insight[0]->values[0]->value; 272 | $processedResult[$post->id]['likes'] = isset($insight->like) ? $insight->like : 0; 273 | $processedResult[$post->id]['shares'] = isset($insight->share) ? $insight->share : 0; 274 | $processedResult[$post->id]['comments'] = isset($insight->comment) ? $insight->comment : 0; 275 | } 276 | } 277 | 278 | return $processedResult; 279 | } 280 | 281 | /** 282 | * Get a post's impressions 283 | * 284 | * @param string $postId 285 | * 286 | * @return int 287 | */ 288 | public function getPostImpressions($postId) 289 | { 290 | return $this->getPostInsight($postId, 'post_impressions')[0]->values[0]->value; 291 | } 292 | 293 | /** 294 | * Get a post's consumptions 295 | * 296 | * @param string $postId 297 | * 298 | * @return int 299 | */ 300 | public function getPostConsumptions($postId) 301 | { 302 | return $this->getPostInsight($postId, 'post_consumptions')[0]->values[0]->value; 303 | } 304 | 305 | /** 306 | * Get a specific insight for a post 307 | * 308 | * @param string $insight 309 | * @param string $postId 310 | * 311 | * @return array 312 | */ 313 | public function getPostInsight($postId, $insight) 314 | { 315 | $queryResult = $this->performGraphCall('/insights/' . $insight, [], $postId); 316 | 317 | return $queryResult->getProperty('data')->asArray(); 318 | } 319 | 320 | /** 321 | * Construct a facebook request 322 | * 323 | * @param string $query 324 | * @param array $params (optional) 325 | * @param string $method (optional) 326 | * @param string $object (optional) 327 | * 328 | * @return GraphObject 329 | */ 330 | public function performGraphCall($query, $params = [], $object = null, $method = 'GET') 331 | { 332 | if (count($params) > 0) { 333 | $i = 0; 334 | 335 | foreach($params as $key => $param) { 336 | if ($i == 0) { 337 | $query .= '?' . $key . '=' . $param; 338 | } 339 | else { 340 | $query .= '&' . $key . '=' . $param; 341 | } 342 | 343 | $i++; 344 | } 345 | } 346 | 347 | $object ? $object = '/' . $object : $object = '/' . $this->pageId; 348 | 349 | $cacheName = $this->determineCacheName([$query, $method, $object]); 350 | 351 | if ($this->useCache() && Cache::has($cacheName)) { 352 | $response = Cache::get($cacheName); 353 | } 354 | else { 355 | $response = (new FacebookRequest( 356 | $this->session[$this->pageId], $method, $object . $query 357 | ))->execute()->getGraphObject(); 358 | 359 | if ($this->useCache()) { 360 | Cache::put($cacheName, $response, $this->config->get('facebook-insights.cache-lifetime')); 361 | } 362 | } 363 | 364 | return $response; 365 | } 366 | 367 | /** 368 | * get the values for an API call between a given date range 369 | * 370 | * @param \DateTime $startDate 371 | * @param \DateTime $endDate 372 | * @param string $query 373 | * @param array $params 374 | * $param bool $values (return an array with values or not) 375 | * 376 | * @return array 377 | */ 378 | public function getDataForDateRange(\DateTime $startDate, \DateTime $endDate, $query, $params = [], $object = null, $values = true) 379 | { 380 | $diff = $startDate->diff($endDate)->days; 381 | 382 | $noQueries = ceil($diff / $this->maxDaysPerQuery); 383 | 384 | if ($noQueries > $this->config->get('facebook-insights.api-call-max')) { 385 | throw new FacebookSDKException('API calls needed for this query exceed "api-calls-max" set in the config file.'); 386 | } 387 | 388 | $data = []; 389 | 390 | if ($noQueries > 1) { 391 | $leftOver = $diff % $this->maxDaysPerQuery; 392 | 393 | for ($i = 1; $i <= $noQueries; $i++) { 394 | if ($i == $noQueries && $leftOver > 0) { 395 | $intervalStartDate = $startDate; 396 | } 397 | else { 398 | $intervalStartDate = clone $endDate; 399 | $intervalStartDate->sub(new \DateInterval('P' . ($this->maxDaysPerQuery * $i) . 'D')); 400 | } 401 | 402 | $intervalEndDate = clone $endDate; 403 | $intervalEndDate->sub(new \DateInterval('P' . ($this->maxDaysPerQuery * ($i - 1)) . 'D')); 404 | 405 | $params['since'] = strtotime($intervalStartDate->format('Y-m-d')); 406 | $params['until'] = strtotime($intervalEndDate->format('Y-m-d')); 407 | 408 | $queryResult = $this->performGraphCall($query, $params, $object)->getProperty('data'); 409 | 410 | if (!is_null($queryResult)) { 411 | $queryResult = $queryResult->asArray(); 412 | $queryResult = $values && isset($queryResult[0]) ? $queryResult[0]->values : $queryResult; 413 | $data = array_merge($data, $queryResult); 414 | } 415 | 416 | if (isset($params['limit']) && count($data) >= $params['limit']) { 417 | $data = array_slice($data, 0, $params['limit']); 418 | break; 419 | } 420 | } 421 | } 422 | else { 423 | $params['since'] = strtotime($startDate->format('Y-m-d')); 424 | $params['until'] = strtotime($endDate->format('Y-m-d')); 425 | 426 | $queryResult = $this->performGraphCall($query, $params, $object)->getProperty('data'); 427 | 428 | if (!is_null($queryResult)) { 429 | $queryResult = $queryResult->asArray(); 430 | $queryResult = $values && isset($queryResult[0]) ? $queryResult[0]->values : $queryResult; 431 | $data = $queryResult; 432 | } 433 | 434 | } 435 | 436 | return $data; 437 | } 438 | 439 | /** 440 | * Calculate totals from an array of raw data by period 441 | * 442 | * @param array $rawData 443 | * 444 | * @return int 445 | */ 446 | private function calculateTotal(array $rawData) 447 | { 448 | $total = 0; 449 | 450 | foreach ($rawData as $data) { 451 | $total += $data->value; 452 | } 453 | 454 | return $total; 455 | } 456 | 457 | /** 458 | * Determine the cache name for the set of query properties given 459 | * 460 | * @param array $properties 461 | * @return string 462 | */ 463 | private function determineCacheName(array $properties) 464 | { 465 | return 'jonasva.facebook-insights.' . md5(serialize($properties)); 466 | } 467 | 468 | /** 469 | * Determine whether or not to cache API responses 470 | * 471 | * @return bool 472 | */ 473 | private function useCache() 474 | { 475 | return $this->config->get('facebook-insights.cache-lifetime') > 0; 476 | } 477 | 478 | } 479 | -------------------------------------------------------------------------------- /src/Jonasva/FacebookInsights/FacebookInsightsServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 23 | __DIR__ . '/../../config/config.php' => config_path('facebook-insights.php'), 24 | ]); 25 | } 26 | 27 | /** 28 | * Register the service provider. 29 | * 30 | * @return void 31 | */ 32 | public function register() 33 | { 34 | $this->app['facebook-insights'] = $this->app->share(function($app) 35 | { 36 | return new FacebookInsights($app['config']); 37 | }); 38 | } 39 | 40 | /** 41 | * Get the services provided by the provider. 42 | * 43 | * @return array 44 | */ 45 | public function provides() 46 | { 47 | return array('facebook-insights'); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/config/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonasva/laravel-facebook-insights/942800a8d4a3dd82f1144128524514a791c3a048/src/config/.gitkeep -------------------------------------------------------------------------------- /src/config/config.php: -------------------------------------------------------------------------------- 1 | '', 14 | 15 | /* 16 | |-------------------------------------------------------------------------- 17 | | App secret 18 | |-------------------------------------------------------------------------- 19 | | 20 | | Your app secret 21 | | 22 | */ 23 | 'app-secret' => '', 24 | 25 | /* 26 | |-------------------------------------------------------------------------- 27 | | Page permanent access token 28 | |-------------------------------------------------------------------------- 29 | | 30 | | Your page's permanent access token 31 | | 32 | | See here on how to obtain a permanent access token for your facebook page: 33 | | https://stackoverflow.com/questions/12168452/long-lasting-fb-access-token-for-server-to-pull-fb-page-info 34 | | 35 | */ 36 | 37 | 'access-token' => '', 38 | 39 | /* 40 | |-------------------------------------------------------------------------- 41 | | Page ID 42 | |-------------------------------------------------------------------------- 43 | | 44 | | Your page's Id 45 | | 46 | */ 47 | 48 | 'page-id' => '', 49 | 50 | /* 51 | |-------------------------------------------------------------------------- 52 | | API call limit per query 53 | |-------------------------------------------------------------------------- 54 | | 55 | | The maximum number of API calls one query is allowed to make 56 | | This applies to queries made to get data from extended period of time. For example: if you make a query 57 | | with a date range of over 92 days, it will split the query in several API calls that each fetch a part of 58 | | the date range. (93 days is the date range limit on the Facebook Graph API) 59 | | 60 | */ 61 | 62 | 'api-call-max' => 15, 63 | 64 | /* 65 | |-------------------------------------------------------------------------- 66 | | Cache lifetime 67 | |-------------------------------------------------------------------------- 68 | | 69 | | The amount of time (in minutes) Graph API responses will be cached. 70 | | If you set this to zero, the responses won't be cached at all. 71 | | 72 | */ 73 | 74 | 'cache-lifetime' => 60 * 24, 75 | ); -------------------------------------------------------------------------------- /tests/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonasva/laravel-facebook-insights/942800a8d4a3dd82f1144128524514a791c3a048/tests/.gitkeep --------------------------------------------------------------------------------