xruff / googleanalytics
Nette 扩展 - Google Analytics 代码
v2.0.4
2023-01-13 11:19 UTC
Requires
- nette/application: ^3.0
- nette/security: ^3.0
Requires (Dev)
- nette/tester: ^2.0
- tracy/tracy: ^2.6
- vanio/coding-standards: ^0.1@dev
README
要求
该软件包需要 PHP 5.6 或更高版本
安装
安装 XRuff/GoogleAnalytics 的最佳方式是使用 Composer
$ composer require xruff/googleanalytics
文档
在 config.neon 中进行配置。两个参数都是可选的。
如果缺少参数 code
,则 GA 代码不会在模板中渲染。如果缺少参数 user
,则使用默认设置 - 值 on
。如果参数 user
是 on
且用户已登录,则 google analytics 代码将包含指令 ga('set', 'userId', {$user->id});
extensions: googleAnalytics: XRuff\App\UI\Components\DI\GoogleAnalyticsExtension googleAnalytics: code: UA-123456789-1 user: on # on/off - display user ID if user is logged in
基础演示者
use XRuff\App\UI\Components\IGoogleAnalyticsControlFactory; abstract class BasePresenter extends Nette\Application\UI\Presenter { /** @var IGoogleAnalyticsControlFactory $googleAnalyticsControlFactory @inject */ public $googleAnalyticsControlFactory; protected function createComponentGoogleAnalytics() { return $this->googleAnalyticsControlFactory->create(); } }
@layout.latte
... </head> <body> {control googleAnalytics} ...
您可以在 latte 模板中调用参数到控件中。第一个参数是 GA 代码。第二个参数应该是布尔值,用于管理为用户会话显示 GA 指令 - ga('set', 'userId', {$user->id});
。两个参数都是可选的。
... </head> <body> {control googleAnalytics 'UA-123456789-2', false} ...