2015-01-13 32 views
0

首先,我需要將數據從Magento導出到JSON或CSV。然後需要將這些數據導入Mixpanel。 Mixpanel提供演示(見下文)。班級和其他一切似乎都很好,但是我很難從Magento獲得實際的數據。我嘗試了很多腳本等,但無法工作。有任何想法嗎?Magento數據導出到JSON然後導入到Mixpanel

<?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); 
    } 
} 


?> 
+0

更多信息對此有所幫助:您打算在Magento所在服務器上的哪個位置執行此操作?作爲一個Magento模塊?通過API調用Magento?其他? –

+0

因此,從我讀過的內容來看,它是一種將腳本中的數據發送到Mixpanel Endpoint API的腳本。事實上,它將與Magento在同一臺服務器上。爲了向您提供更多信息,請點擊這裏鏈接到Mixpanels文章:https://mixpanel.com/docs/api-documentation/importing-events-older-than-31-days - 我也可以爲您提供這些功能爲我提供了Magento的日常數據。從我與Mixpanel支持交談開始,數據就變成了JSON或CSV格式。 – damek132

+0

考慮到最乾淨的是做一個Magento模塊,你可能想從這裏開始http://stackoverflow.com/questions/576908/how-to-create-a-simple-hello-world-module-in- magento,然後用特定的問題來優化你的問題。 –

回答

0

如果您將數據存儲在CSV文件中,則可以使用分段CSV導入器輕鬆導入到Mixpanel。這個工具是建立在Segment之上的,如果你已經在使用Segment,這個過程會更容易。要導入數據和csv文件規範,請訪問here