heimrichhannot / contao-social-stats-bundle
此插件收集新闻条目的数据。
0.1.4
2023-01-10 15:26 UTC
Requires
- php: ^7.4 || ^8.0
- contao/core-bundle: ^4.9
- contao/news-bundle: ^4.4
- guzzlehttp/guzzle: ^6.0 || ^7.0
- symfony/config: ^4.4 || ^5.4
- symfony/console: ^4.4 || ^5.4
- symfony/dependency-injection: ^4.4 || ^5.4
- symfony/event-dispatcher: ^4.4 || ^5.4
- symfony/http-kernel: ^4.4 || ^5.4
README
此插件收集新闻条目的数据。目前它包含一个命令,用于更新来自某些网络新闻文章的一些统计数据。
功能
- 以下统计数据被收集
- Google Analytics:唯一页面浏览量
- Facebook:分享次数
- Matomo:点击次数
设置
要求
- PHP 7.2 或更高版本(未在 8.0 和更高版本上测试)
- Contao 4.4 或更高版本
附加
- Google Analytics: PHP Google API 客户端库
安装
-
使用 composer 或 Contao 管理员进行安装
composer require heimrichhannot/contao-social-stats-bundle
-
更新数据库
php vendor/bin/contao-console contao:migrate
-
为 SocialStatsCommand 设置 cron 作业(有关更多信息,请参阅用法 -> 命令)
* */1 * * * php vendor/bin/contao-console huh:socialstats:update
配置
大多数平台都需要添加配置,例如访问令牌。请参阅配置参考以了解所需内容。
用法
命令
Usage:
huh:socialstats:update [options]
Options:
-p, --platforms[=PLATFORMS] Limit to specific platform/network. See help for more information.
-l, --limit=LIMIT Limit the number of news article to update. [default: 20]
-a, --age=AGE Limit the age of articles to be updated to a number of days. 0 means no limit. [default: 0]
--pid=PID Limit the news articles to given archives. 0 means all archives. [default: 0]
--dry-run Don't write anything to the database. API-Calls are still executed.
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-e, --env=ENV The Environment name. [default: "prod"]
--no-debug Switches off debug mode.
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Help:
This command updates the social statistics of your news entries.
Following options are available for platforms option:
fb - Facebook
ga - Google Analytics
ma - Matomo
处理值
命令收集的值被写入到 tl_news 的 huh_socialstats_values
字段。您可以通过简单的 \Contao\StringUtil::deserialze($newsItemModel->huh_socialstats_values, true)
获取它们。统计数据最后更新的时间被写入到 tl_news.huh_socialstats_last_updated
。
Google Analytics
我们使用 PHP Google API 客户端库 来获取值。为此,您需要
- 一个 Google API 控制台项目
- 一个密钥文件
- 一个视图 ID
您在此处可以找到获取这些组件的所有信息: https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/web-php
添加自定义新闻路由
默认情况下,平台将搜索默认新闻 URL(新闻存档中设置的页面加上一个标识符,通常是新闻别名,见 \Contao\News::generateNewsUrl()
)。如果您在多个位置输出新闻或有一些自定义路由,您可以使用 AddNewsArticleUrlsEvent
添加或更改 URL。
use HeimrichHannot\SocialStatsBundle\Event\AddNewsArticleUrlsEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class SocialStatsEventSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents() { return [ AddNewsArticleUrlsEvent::class => 'addNewsArticleUrls', ]; } public function addNewsArticleUrls(AddNewsArticleUrlsEvent $event) { $event->addUrl($event->getBaseUrl().'/my_custom_news_article_route/'.$event->getItem()->id); } }
配置参考
# Default configuration for extension with alias: "huh_social_stats" huh_social_stats: # Override the auto-determined base url. base_url: null # The start date from which data should be counted. Needed for analytics services like matomo or google analytics. Default values is 2005-01-01 as timestamp. start_date: 1104534000 matomo: # Set the matomo url url: ~ # The matomo authorization token token: ~ facebook: # The facebook app id. app_id: null # The facebook app secret. app_secret: null google_analytics: # View ID view_id: ~ # Relative path to the google analytics keyfile. key_file: files/bundles/huh_social_stats/google_analytics/privatekey.json