Reputation: 582
I have this application running on PHP's CodeIgniter framework. I'm trying to setup people tracking with Mixpanel library. So I can know users who logged in and when they did it.
I've installed this library's files on my application/libraries folder, and then called it on my controller login.php like this:
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));
}
After that, in this same controller (index function), added this:
// 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,
));
The whole function is this:
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 is receiving the login information (because I can see it in the dashboard), but my application is returning a few errors:
Severity: Warning
Message: Missing argument 1 for Mixpanel::__construct(), called in /Applications/MAMP/htdocs/descola-rep/system/core/Loader.php on line 1099 and defined
Filename: libraries/Mixpanel.php
Line Number: 138
Severity: Notice
Message: Undefined variable: token
Filename: libraries/Mixpanel.php
Line Number: 140
Severity: Notice
Message: Undefined variable: token
Filename: libraries/Mixpanel.php
Line Number: 141
Severity: Warning
Message: Creating default object from empty value
Filename: controllers/login.php
Line Number: 11
I would be very glad if someone could give me some hint of where am I am going wrong. I'm still learning back end development, I can't figure this one.
Cheers
Upvotes: 1
Views: 764
Reputation: 582
So, with a big help from Mixpanels guys (btw they are awesome), I could get to this solution. For those who will ever face this same problem, here's the thing:
1) Install the library's files on application/libraries/mixpanel_lib
2) Create a new php file in application/libraries called Mixpanel_wrapper.php. There you'll recquire your library path, set your token and create some functions. The file below already has some functions as track_something, people_set and indentify. For further information, just add the functions here.
<?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) Load the library wherever you'll need to use it. Like this example on my login.php controller:
public function __construct(){
parent::__construct();
$this->load->model('login_model');
$this->load->library('form_validation');
$this->load->library('mixpanel_wrapper');
}
4) Track something!
// 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
I think that's it. :)
Upvotes: 3
Reputation: 218
as it seems, there is an issue with your library integration in CodeIgniter. Maybe this link will help: https://github.com/bpartridge83/codeigniter-bootstrap/blob/master/application/libraries/Mixpanel.php
Upvotes: 1