0

First I need to export data from Magento into JSON or CSV. Then this data needs to be imported to Mixpanel. Mixpanel provides the demo (see below). the class and everything else seems fine, but the actual data I struggle to get from Magento. I tried a lot of scripts etc, but cant get nothing to work. Any ideas?

<?php
/*
Example script that will import $born events into your Mixpanel projects for your existing users.

Feel free to modify. If you have a high number of users you might want to use some sort of queing system instead of 
a sleep command to make sure that all the requests get sent propertly. 
*/

/*
Dummy Data:
This array represents the data you would fetch from your own database that would have user_ids and the date you would like to set as their 
birthdate. Mixpanel will use this birthdate to determine their cohort. 
*/

$users = array(
    "47859"=>"2011-01-07 12:23:01",
    "47860"=>"2011-01-07 13:43:47",
    "47861"=>"2011-01-07 13:54:24",
    "47862"=>"2011-01-08 20:12:23",
    "47863"=>"2011-01-08 22:59:29",
);

date_default_timezone_set("America/New_York"); //set for the timezone your sign up data is using. 

//Constructer: new EventImporter("Project Token","Project API Key");
//Both these values are on your mixpanel accounts page: http://mixpanel.com/account/
$metrics = new EventImporter("TOKEN_HERE","API_KEY_HERE");

foreach($users as $id=>$birthdate){
 $props = array();
 $props['distinct_id'] = $id; //distinct_id should be your identifier
 $props['time'] = strtotime($birthdate); //time should be their $birthdate
 $event = '$signup'; //you are sending the $signup event. You could also put $born here. 
 echo "\nSending $event event for ".$props['distinct_id']." at $birthdate (".$props['time'].")\n";

 $metrics->track($event, $props);
}


class EventImporter {
    public $token;
    public $api_key;
    public $host = 'http://api.mixpanel.com/';
    public function __construct($token_string,$api_key) {
        $this->token = $token_string;
        $this->api_key = $api_key;
    }
    function track($event, $properties=array()) {
        $params = array(
            'event' => $event,
            'properties' => $properties
            );

        if (!isset($params['properties']['token'])){
            $params['properties']['token'] = $this->token;
        }
        $url = $this->host . 'import/?data=' . base64_encode(json_encode($params)) . "&api_key=$this->api_key";
        //you still need to run as a background process
        echo "$url\n";
        exec("curl '" . $url . "' >/dev/null 2>&1 &"); 
        sleep(.2);
    }
}


?>
pasujemito
  • 312
  • 4
  • 16
  • More info would help here : where are you planning to do this on the same server as Magento ? As a Magento module ? via API call to Magento ? Else ? – β.εηοιτ.βε Jan 13 '15 at 14:37
  • So as from what I have read, its a script of some sort that send data from the script to Mixpanel Endpoint API. Indeed it would be on the same server as Magento. To provide you with a little bit more info here is the link to Mixpanels article regarding this: https://mixpanel.com/docs/api-documentation/importing-events-older-than-31-days - I can also provide you with the functions that provide me daily data from Magento. As from when I spoke with Mixpanel Support the data got to be in JSON or CSV. – pasujemito Jan 13 '15 at 15:05
  • Well considering that the cleanest is to do a Magento module, you may want to start from here http://stackoverflow.com/questions/576908/how-to-create-a-simple-hello-world-module-in-magento and then refine your question with specific problem. – β.εηοιτ.βε Jan 13 '15 at 15:12
  • Am sure this can be done with just one script, as this is not a continuous function, more like a one off that does this job. – pasujemito Jan 13 '15 at 16:30

1 Answers1

0

If you have your data in a CSV file, you can easily import to Mixpanel with Segment CSV Importer. This tool is built on top of Segment, if you are already using Segment, this process will be more easier. For importing data and csv file specifications visit here.

user3058327
  • 1
  • 1
  • 1