Mixpanel人员跟踪我的PHP CodeIgniter应用程序

时间:2015-04-21 15:44:33

标签: php codeigniter mixpanel

我在PHP的CodeIgniter框架上运行此应用程序。我正在尝试使用Mixpanel library设置人员跟踪。所以我可以知道登录的用户以及他们何时登录。

我在我的应用程序/库文件夹中安装了这个库的文件,然后在我的控制器login.php上调用它,如下所示:

public function __construct(){
    parent::__construct();
    $this->load->model('login_model');
    $this->load->library('form_validation');
    // Mixpanel Config
    $this->load->library('Mixpanel');
    $this->Mixpanel->mp = Mixpanel::getInstance("9966a1a78b347f556a7cc0c9f298502b", array("use_ssl" => false));

}

之后,在同一个控制器(索引函数)中添加了这个:

// Sends login information to Mixpanel
            $this->Mixpanel->mp->people->identify($aluno[0]->aluno_id, array(
        '$first_name'       => $aluno[0]->aluno_primeiro_nome,
        '$last_name'        => $aluno[0]->aluno_sobrenome,
        '$email'            => $aluno[0]->aluno_email,
            ));

整个功能是:

public function index(){
    $this->form_validation->set_error_delimiters('<p class="default_error">', '</p>');
    $this->form_validation->set_rules('user_email', 'E-mail', 'trim|required|valid_email|xss_clean');
    $this->form_validation->set_rules('user_password', 'Senha', 'trim|required|xss_clean');
    $this->form_validation->set_message('required', 'Campo de preenchimento obrigatório');
    $this->form_validation->set_message('valid_email', 'Por favor, digite um e-mail válido');
    if ($this->form_validation->run()){
        if($this->login_model->authenticate($this->input->post('user_email'), $this->input->post('user_password'))){
            $this->load->model('aluno_model');
            $aluno = $this->aluno_model->getStudentbyEmail($this->input->post('user_email'));
            if(!empty($aluno[0]->aluno_thumb_img)) :
            $login = array(
                        'id' => $aluno[0]->aluno_id, 
                        'primeiro_nome' => $aluno[0]->aluno_primeiro_nome, 
                        'sobrenome' => $aluno[0]->aluno_sobrenome,
                        'senha' => $aluno[0]->aluno_senha, 
                        'email' => $aluno[0]->aluno_email, 
                        'thumb' => base_url('student_images/'.$aluno[0]->aluno_thumb_img),
                        'large' => base_url('student_images/'.$aluno[0]->aluno_large_img),
                        'status' => $aluno[0]->aluno_status
                        );
            else :
            $login = array(
                        'id' => $aluno[0]->aluno_id, 
                        'primeiro_nome' => $aluno[0]->aluno_primeiro_nome, 
                        'sobrenome' => $aluno[0]->aluno_sobrenome,
                        'senha' => $aluno[0]->aluno_senha, 
                        'email' => $aluno[0]->aluno_email, 
                        'thumb' => base_url('student_images/'.$aluno[0]->aluno_large_img),
                        'large' => base_url('student_images/'.$aluno[0]->aluno_large_img),
                        'status' => $aluno[0]->aluno_status
                        );
            endif;

            // Sends login information to Mixpanel
            $this->Mixpanel->mp->people->identify($aluno[0]->aluno_id, array(
        '$first_name'       => $aluno[0]->aluno_primeiro_nome,
        '$last_name'        => $aluno[0]->aluno_sobrenome,
        '$email'            => $aluno[0]->aluno_email,
            ));

            $this->session->set_userdata('user', $login);

            if($this->input->post('url_checkout')){
                $this->set_flashdata('loginSuccess', 'loginSuccess', $this->input->post('url_checkout'));
            }else{
                $this->set_flashdata('loginSuccess', 'loginSuccess', base_url('aluno'));
            }
        }else{
            $this->set_flashdata('loginFailed', 'loginFailed', $this->input->post('url'));
        }
    }else{
        $errors = array('user_email'=>form_error('user_email'), 'user_password'=>form_error('user_password'));
        $this->set_flashdata('loginError', $errors, $this->input->post('url'));
    }

}

Mixpanel正在接收登录信息(因为我可以在仪表板中看到它),但我的应用程序返回了一些错误:

遇到PHP错误

严重性:警告

消息:缺少参数1的Mixpanel :: __ construct(),在1099行的/Applications/MAMP/htdocs/descola-rep/system/core/Loader.php中调用并定义

文件名:libraries / Mixpanel.php

行号:138

遇到PHP错误

严重性:注意

消息:未定义的变量:标记

文件名:libraries / Mixpanel.php

行号:140

遇到PHP错误

严重性:注意

消息:未定义的变量:标记

文件名:libraries / Mixpanel.php

行号:141

遇到PHP错误

严重性:警告

消息:从空值

创建默认对象

文件名:controllers / login.php

行号:11

如果有人能给我一些暗示我出错的地方,我会很高兴的。我还在学习后期开发,我无法想到这一点。

干杯

2 个答案:

答案 0 :(得分:3)

所以,在Mixpanels家伙的大力帮助下(顺便说一句,他们很棒),我可以得到这个解决方案。对于那些将面临同样问题的人来说,就是这样:

1)在application / libraries / mixpanel_lib上安装库的files

2)在名为Mixpanel_wrapper.php的应用程序/库中创建一个新的php文件。在那里,您将获得您的库路径,设置您的令牌并创建一些功能。下面的文件已经有一些功能,如track_something,people_set和identify。有关详细信息,请在此处添加功能。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
require 'application/libraries/mixpanel_lib/Mixpanel.php';

class Mixpanel_wrapper {

private $mixpanel = false;

public function __construct(){
  $mp = $this->getMixpanel();
    $mp = Mixpanel::getInstance("PLACE_YOUR_TOKEN_HERE");
    $this->setMixpanel($mp);
}

public function getMixpanel(){ return $this->mixpanel; }
public function setMixpanel($obj){ $this->mixpanel =  $obj; }

public function track_something($event = '', $properties = array()){
    $mp = $this->getMixpanel();

    $mp->track($event, $properties);

}// track_something function ends

public function people_set($distinct_id = '', $properties = array()){

    $mp = $this->getMixpanel();

    $mp->people->set($distinct_id, $properties);

}// people_set function ends

 public function identify($distinct_id){

  $mp = $this->getMixpanel();

  $mp->identify($distinct_id);

 }// identify function ends

}/* End of file mixpanel_wrapper.php */

3)将库加载到需要使用它的任何地方。就像我的login.php控制器上的这个例子一样:

public function __construct(){
    parent::__construct();
    $this->load->model('login_model');
    $this->load->library('form_validation');
    $this->load->library('mixpanel_wrapper');
}

4)跟踪某事!

// Sends login information to mixpanel
            $this->mixpanel_wrapper->people_set($student[0]->student_id, array(
            '$first_name'       => $student[0]->student_first_name,
            '$last_name'        => $student[0]->student_last_name,
            '$email'            => $student[0]->student_email,
            ));
            $this->mixpanel_wrapper->identify($student[0]->student_id);
            $this->mixpanel_wrapper->track_something('Logged in');
            // ends mixpanel

我认为就是这样。 :)

答案 1 :(得分:1)

看起来,CodeIgniter中的库集成存在问题。也许这个链接会有所帮助:https://github.com/bpartridge83/codeigniter-bootstrap/blob/master/application/libraries/Mixpanel.php