okatsuralau / cakephp-mixpanel
CakePHP 插件,提供与 Mixpanel 分析的简单集成
v1.0.4
2017-05-10 21:06 UTC
Requires
- php: >=5.6
- mixpanel/mixpanel-php: ^2.6
Requires (Dev)
- cakephp/cakephp: ^3.2
- phpunit/phpunit: ^6.1
This package is not auto-updated.
Last update: 2024-09-29 03:00:47 UTC
README
CakePHP Mixpanel 插件
此插件提供了一个 Mixpanel 组件,用于跟踪控制器中的事件,使用官方的 Mixpanel PHP 库。
要求
- PHP >= 5.6
- CakePHP >= 3.0
如何安装
composer require okatsuralau/cakephp-mixpanel@1.0.0
如何使用
在您的 config/bootstrap.php
文件中加载插件
Plugin::load('CakephpMixpanel');
在您的 config/app.php
或 config/app_custom.php
文件中添加插件配置
return [ //... 'Mixpanel' => [ 'token' => YOUR_TOKEN_HERE ] ]
在您的 src/Controller/AppController.php
中加载组件
public function initialize() { parent::initialize(); $this->loadComponent('CakephpMixpanel.Mixpanel'); }
并且(可选)将以下代码添加到您的 beforeFilter()
方法中以识别用户的行为
public function beforeFilter(\Cake\Event\Event $event) { // if a user is logged in $this->Mixpanel->identify($user_id); $this->Mixpanel->name_tag($user_name); $this->Mixpanel->register($superProperties); /* To make use of the people API */ $this->Mixpanel->people($this->Auth->user('id'), array( '$username' => $this->Auth->user('username'), '$email' => $this->Auth->user('email'), '$created' => $this->Auth->user('created'), '$last_login' => $this->Auth->user('connected'), 'my_custom_var' => $my_custom_var, )); // ... parent::beforeFilter($event); }
要在控制器操作中注册事件,将以下代码放入您的控制器操作中。 src/Controller/PostController.php
public function index() { // ... $this->Mixpanel->track( 'Post list', [ 'author' => $this->Auth->user('name'), 'category' => 'Post', ] ); } public function create() { if ($this->request->is('post')) { // ... $this->Mixpanel->track( 'Post Created', [ 'author' => $this->Auth->user('name'), 'category' => 'Post', ] ); $this->redirect(array('action'=>'index')); } }
这应该足以开始向 Mixpanel 发送事件。
许可证
版权所有 2017 Gabriel Lau
根据 MIT 许可证提供给您使用。请参阅: https://open-source.org.cn/licenses/MIT