jonasva/laravel-facebook-insights

Facebook 页面洞察与 Laravel 集成

dev-master 2015-08-10 08:52 UTC

This package is auto-updated.

Last update: 2024-09-14 18:20:20 UTC


README

FacebookInsights 提供了一种快速访问 Facebook 页面洞察的方法,它使用 Facebook OpenGraph API v2。它通过永久访问令牌工作,因此不需要用户交互。常见的用法是有一个需要定期抓取 Facebook 页面洞察的统计仪表板。

安装

要获取 FacebookInsights 的最新版本,请在 composer.json 文件中添加它。

"jonasva/laravel-facebook-insights": "dev-master"

(对于 Laravel 4,请参阅此存储库 Laravel4 分支的文档)

运行 composer update jonasva/laravel-facebook-insights 安装它。

FacebookInsights 安装后,您需要将其服务提供者注册到您的应用程序中。打开 config/app.php 并找到 providers 键。

'providers' => array(

    Jonasva\FacebookInsights\FacebookInsightsServiceProvider::class,

)

还包括一个用于轻松访问的 Facade。您可以在 config/app.php 文件的 aliases 键中注册 Facade。

'aliases' => array(

    'FacebookInsights' => Jonasva\FacebookInsights\Facades\FacebookInsights::class,

)

发布配置

从您的项目根目录的命令行运行此操作

$ php artisan vendor:publish

一个配置文件将发布到 config/facebook-insights.php

配置

Facebook 应用和页面信息

要使用此包,您需要设置 Facebook 应用 ID、应用密钥、(永久)访问令牌和页面 ID。有关更多信息,请检查配置文件。

缓存

Facebook GraphApi 响应默认缓存 1 天。您可以通过更改 cache-lifetime 来更改此设置。

用法

此包包含一些有用的方法来使用 OpenGraph API 获取 Facebook 洞察。可以通过使用 Facade FacebookInsights 来调用方法。例如

    use FacebookInsights; // this goes above your class declaration

    // ...

    $startDate = new \DateTime('2015-03-15');
    $endDate = new \DateTime('2015-03-25');
    // fetch your page's total impressions for a given period
    $totalImpressions = FacebookInsights::getPageTotalImpressions($startDate, $endDate);

方法

此包目前提供页面和帖子对象的洞察。换句话说,也可以通过以下方法执行任何其他 OpenGraph 查询

    /**
     * Construct a facebook request
     *
     * @param string $query
     * @param array $params (optional)
     * @param string $method (optional)
     * @param string $object (optional)
     *
     * @return GraphObject
     */
    public function performGraphCall($query, $params = [], $object = null, $method = 'GET')

页面洞察

获取页面粉丝总数(也称为关注者、喜欢页面的用户)

    /**
     * Get the total amount of page fans (people who liked the page)
     *
     * @return int
     */
    public function getPageTotalFans()

获取特定时间段内每天的新粉丝数量

    /**
     * Get new page fans per day for a given period
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     *
     * @return array
     */
    public function getPageNewFansPerDay(\DateTime $startDate, \DateTime $endDate)

获取特定时间段内页面新粉丝的总数

    /**
     * Get the total number of new page fans for a given period
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     *
     * @return int
     */
    public function getPageTotalNewFans(\DateTime $startDate, \DateTime $endDate)

获取特定时间段内每天的页面曝光量(与您的页面相关联的任何内容的总曝光量)

    /**
     * Get the page impressions per day for a given period
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     *
     * @return array
     */
    public function getPageImpressionsPerDay(\DateTime $startDate, \DateTime $endDate)

获取特定时间段内页面曝光量的总数

    /**
     * Get the total number of page impressions for a given period
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     *
     * @return int
     */
    public function getPageTotalImpressions(\DateTime $startDate, \DateTime $endDate)

获取特定时间段内每天的页面消耗量(人们点击您内容次数的总数)

    /**
     * Get the page consumptions per day for a given period
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     *
     * @return array
     */
    public function getPageConsumptionsPerDay(\DateTime $startDate, \DateTime $endDate)
    {

获取特定时间段内页面消耗量的总数

    /**
     * Get the total number of page consumptions for a given period
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     *
     * @return int
     */
    public function getPageTotalConsumptions(\DateTime $startDate, \DateTime $endDate)

获取页面帖子的点赞、评论、分享、RSVP、认领和回答计数,按天分组,针对特定时间段

    /**
     * Get a page's positive feedback per day for a given period
     * The following actions are categorized as positive feedback:
     * like, comment, link (share), rsvp (respond to an event), claim, answer
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     *
     * @return array
     */
    public function getPagePositiveFeedbackPerDay(\DateTime $startDate, \DateTime $endDate)

获取页面帖子的累计(总数)点赞、评论、分享、RSVP、认领和回答计数,按天分组,针对特定时间段

    /**
     * Get a page's accumulated positive feedback for a given period
     * The following actions are categorized as positive feedback:
     * like, comment, link (share), rsvp (respond to an event), claim, answer
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     *
     * @return array
     */
    public function getPageTotalPositiveFeedback(\DateTime $startDate, \DateTime $endDate)

获取特定时间段内页面的特定洞察。洞察可以在这里找到:https://developers.facebook.com/docs/graph-api/reference/v2.2/insights#page_impressions

    /**
     * Get a specific insight for a page for a given period
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     * @param string $insight
     * @param string $period (optional)
     *
     * @return int
     */
    public function getPageInsight(\DateTime $startDate, \DateTime $endDate, $insight, $period = 'day')

获取特定时间段内页面的帖子。这实际上不是洞察,但需要获取帖子 ID,稍后可以使用这些 ID 来收集帖子洞察。

    /**
     * Get the page's posts for a given period
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     * @param int $limit
     *
     * @return array
     */
    public function getPagePosts(\DateTime $startDate, \DateTime $endDate, $limit = null)

切换到另一个页面

还可以动态切换到另一个页面,以获取其洞察/帖子。您可以使用 switchPage 方法来这样做

    /*
     * Switch to another page to get insights of
     *
     * @param string $pageId
     * @param string $accessToken
     */
    public function switchPage($pageId, $accessToken)

示例

    FacebookInsights::switchPage('other page id', 'other page's permanent access token');

帖子洞察

特定洞察只能按终身时间段收集,因此无需提供日期范围。

获取帖子的曝光次数

    /**
     * Get a post's impressions
     *
     * @param string $postId
     *
     * @return int
     */
    public function getPostImpressions($postId)

获取帖子的消费次数

    /**
     * Get a post's consumptions
     *
     * @param string $postId
     *
     * @return int
     */
    public function getPostConsumptions($postId)

获取帖子的特定洞察。帖子洞察可以在以下位置找到:https://developers.facebook.com/docs/graph-api/reference/v2.2/insights#post_impressions

    /**
     * Get a specific insight for a post
     *
     * @param string $insight
     * @param string $postId
     *
     * @return array
     */
    public function getPostInsight($postId, $insight)

获取指定时间段内具有计算洞察的页面帖子

    /**
     * Get the page's posts with calculated insights for a given period
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     * @param int $limit
     *
     * @return array
     */
    public function getPagePostsBasicInsights(\DateTime $startDate, \DateTime $endDate, $limit = null)