Magento 混合面板 PHP 库

Magento Mixpanel PHP Library

如何从 Mixpanel Analytics 添加新的 PHP 库到 Magento。

这是图书馆的实际 link: https://mixpanel.com/help/reference/php

在我下载的 github 文件中有一个名为 lib 的文件夹,我已将其重命名为 Mixpanel 并将其放在 Magento 的 /lib 文件夹中 - 但是 Magento 会自动加载它还是我需要添加一行代码如:

require_once(Mage::getBaseDir('lib') . '/Mixpanel/MixpanelBase.php');

第二部分

现在我需要为每个事件从数据库中导入数据。查看演示:http://mpdocs.s3.amazonaws.com/import-old-signup-events-demo.php

在我的文件中有以下内容:

class Mixpanel_Model_Observer extends Mage_Core_Model_Abstract
{
public $token;
public $api_key;
public $host = 'http://api.mixpanel.com/';

public function __construct($token_string,$api_key) {
    $this->token = Mage::getStoreConfig('mixpanel_options/settings/mixpanel_token');
    $this->api_key = Mage::getStoreConfig('mixpanel_options/settings/mixpanel_api_key');
}

public 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";
    exec("curl '" . $url . "' >/dev/null 2>&1 &"); 
}
}

现在我不确定如何使用此代码使其与我已有的功能一起使用,此代码是否可以 运行 用于所有事件,或者我是否需要为所有单独的功能创建此代码(而不是生日我想获取登录数据):

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

这是我跟踪登录的功能:

public function trackCustomerLogin($observer) {

    $this->track('customer_action', array('action'          => 'login',
                                          'distinct_id'     => $this->getCustomerIdentity()));
}

如果您已将 lib 文件夹重命名为 Mixpanel,则应将其添加到 classes 名称作为前缀: 例子: class Base_MixpanelBase

将变为:

class Mixpanel_Base_MixpanelBase

然后通过注释删除所有 require_once() 调用,并且不要忘记通过添加前缀来更改所有 class 名称:Mixpanel_ 到他们的名字,但是不要不要更改文件名。 这样您将遵循 Varien 和 Zend 库的编写方式,您可以像这样直接调用 classes: $mixPanelBase = new Mixpanel_Base_MixpanelBase($options);

没有 require_once 调用。 Magento 将负责自动加载。

Magento 自动加载器会将下划线替换为斜杠并在末尾添加 .php。即:

  • new Some_Class_Name -> require_once 'lib/Some/Class/Name.php'

如果您将文件直接提取到 lib 文件夹中,您应该能够自动加载 Mixpanel 类,例如这些文件应该在

  • /magento/lib/MixpanelBase.php
  • /magento/lib/Base/MixpanelBase.php

等然后,您将能够在 Magento 中使用 $var = new MixpanelBase(); 而无需使用 include 语句。