php
API集成
Mixpanel PHP库设计用于脚本,或者在客户端无法或无法运行客户端脚本的情况下
安装库
您可以使用图书馆使用作曲家通过在项目的Composer中包括以下内容。JSON的要求和运行作曲家更新:
"require": { ... "mixpanel/mixpanel-php" : "2.*" ... }
If you're not using Composer for your package management, you can browse and download the library from GitHub athttps://github.com/mixpanel/mixpanel-php。
我们的图书馆需要PHP 5.0或更高。
EU Data Residency
通过设置一个将数据路由到Mixpanel的欧盟服务器主持人
configuration during initialization.
"api-eu.www.jy710.com"));
Sending Events
Track events in the mixpanel-php library by using the追踪method on the Mixpanel class:
track("button clicked", array("label" => "sign-up"));
MixPanel使用传入请求上的IP地址确定默认地理位置数据($ CITY,$ region,MP_COUNTRY_CODE)。由于所有服务器端调用可能源于相同的IP(即服务器的IP),因此这可能会产生意想不到的效果,即设置所有用户的位置到数据中心的位置。阅读有关使用服务器端实现的地理位置的最佳实践。
Super Properties
超级属性是随后跟踪事件发送的属性。这对于将诸如“广告源”之类的属性与每个事件相关联非常有用。如果我想为我跟踪的每个事件发送“ Google”的“广告源”,则可以将其存储在MixPanel实例中注册方法:
<?php需要“供应商/autoload.php”;$ mp = mixpanel :: getInstance(“ mixpanel_project_token”);// register the Ad Source super property $mp->register("Ad Source", "Google"); // track an event with a property "Ad Source" = "Google" $mp->track("button clicked");
笔记
In the PHP Library, registered Super Properties are only persisted for the life of the Mixpanel class instance. In general this will mean for the life of a single HTTP request.
Super Properties Live in Local Storage
Mixpanel's server-side libraries do not automatically append "super properties" to their events. You are more than welcome to roll your own system to append whatever properties you'd like to events for a given user. The most important thing to note when dealing with appending properties server side is that you must include a value for the (traditionally super) property "distinct_id" in order to use the events in most Mixpanel reports. The distinct_id property ties an event to a specific user.
Managing User Identity
Mixpanelgroups events sent with different distinct_ids, presenting them in reports as different user event streams. You can connect events with different distinct_ids using别名,识别或合并,最终将它们归因于一个用户。
ID合并
如果一个项目有ID合并启用, the
$identify
事件可以连接预验证事件和后验证事件。如果未启用ID合并,则标识事件不会链接身份,但是可以使用别名来连接预注册事件。
存储用户配置文件
In addition to events, you can send user profile updates to Mixpanel. Mixpanel can maintain a profile of each of your users, storing information you know about them. An update is a message that changes the properties of a user profile.
You can use profiles to explore and segment users by who they are, rather than what they did. You can also use profiles to send messages, such as emails, SMS, or push notifications.
MixPanel使用传入请求上的IP地址确定默认地理位置数据($ CITY,$ region,MP_COUNTRY_CODE)。由于所有服务器端调用可能源于相同的IP(即服务器的IP),因此这可能会产生意想不到的效果,即设置所有用户的位置到数据中心的位置。阅读有关使用服务器端实现的地理位置的最佳实践。
设置配置文件属性
Mixpanel班有一个公共财产称为peoplethat exposes an instance ofproducers_mixpanelpeoplethat you can use to make profile updates.
<?php需要“供应商/autoload.php”;$ mp = mixpanel :: getInstance(“ mixpanel_project_token”);//创建或更新具有名称,姓氏,// e-mail地址,电话号码和喜欢的颜色//的配置文件,而无需更新地理位置数据或$ last_seen $ mp-> poys-> set(12345,array('array)('$ first_name'=>“ john”,'$ last_name'=>“ doe”,'$ email'=>”[电子邮件保护]“,'$ phone'=>“ 5555555555”,“ faste color” =>“ red”),$ ip = 0,$ ignore_time = true);
The call topeople->set将在用户12345的配置文件上设置属性的值。如果已经在Mixpanel中没有具有独特的_ID 12345的配置文件,将创建一个新的配置文件。如果用户12345已经在其配置文件上设置了这些属性中的任何一个,则旧值将被新值覆盖。
笔记
Pick your property names wisely. Once you've sent them to Mixpanel, there is no way to change them. Feel free to use capitalization and spaces in between words.
There are a few limitations:
- Your property names should not begin with
$
要么MP_
。这些属性保留给Mixpanel发送的特殊属性。- 您的属性名称不能以空间的形式开始或结束,因为它们会自动修剪。
- Your property names and values cannot be longer than 255 characters. In practice they should be much shorter than that. Property names get cut off by our user interface at about 20 characters.
Click这里要查看Mixpanel保留的用户配置文件属性列表。
Incrementing Numeric Properties
您可以使用数字属性的当前值people->increment。This is useful when you want to keep a running tally of things, such as games played, emails sent, or points earned.
<?php需要“供应商/autoload.php”;$ mp = mixpanel :: getInstance(“ mixpanel_project_token”);// increment user 12345's "login count" by one $mp->people->increment(12345, "login count", 1); // Use negative numbers to subtract- reduce // "credits remaining" by 10 $mp->people->increment(12345, "credits remaining", -10);
Appending to List Properties
采用people->append将项目添加到现有的列表价值属性中。您发送附加的值将添加到每个命名属性的列表末尾。如果该属性不存在,它将以一个元素列表作为其值创建。
<?php需要“供应商/autoload.php”;$ mp = mixpanel :: getInstance(“ mixpanel_project_token”);//将“苹果”附加到用户12345的“收藏夹” $ MP-> People->附加(12345,“ fairite”,“ apples”);
Other Types of Profile Updates
There are a few other types of profile updates. They're exposed as public methods ofproducers_mixpanelpeople。
跟踪收入
Mixpanel使分析您从各个客户中赚取的收入变得容易。通过将费用与用户配置文件相关联,您可以比较不同客户群的收入,并计算诸如终身价值之类的内容。
您可以通过TrackChargeMixpanel-> People对象的方法。发送一条用来创建的消息TrackCharge将向单个用户配置文件添加交易,这将反映在Mixpanel收入报告中。
<?php需要“供应商/autoload.php”;$ mp = mixpanel :: getInstance(“ mixpanel_project_token”);//跟踪用户12345 //的购买或费用为$ 9.99 //交易发生的地方$ MP-> People-> TrackCharge(12345,“ 9.99”);//跟踪用户的购买或费用为$ 20345 //在2013年6月1日在2013年6月1日在est $ mp-> people-> trackcharge(12345,“ 20.00”,strtotime,strtotime(“ 2013年6月1日)5:00):00 pm est”));
调试和伐木
You can turn on Mixpanel logging by enabling the "debug" flag in your initialization:
<?php需要“供应商/autoload.php”;$ mp = mixpanel :: getInstance(“ mixpanel_project_token”,array(“ debug” => true));
缩放服务器端跟踪
消息消费者
PHP库将所有事件和配置文件更新存储在内存队列中,当实例的实例时会自动冲洗Mixpanelis destroyed, or when the queue reaches a configurable threshold size (by default, 1000 items). You can also force the instance to send on demand by callingflush。
在冲洗时,消息是通过实现的commuterStrategies_abstractconsumer确定如何编写消息的类。默认设置使用commuterStrategies_curlconsumer, which uses cURL to write the messages over SSL to Mixpanel.
随着您的应用程序缩放,您可能需要将IO分开以与Mixpanel交流,从观察您的事件的流程中进行交流。您可以通过编写自己的自定义消费者来编写事件并更新到文件或分布式队列。
去创造自定义消费者,您需要扩展commuterStrategies_abstractconsumerand implement the persist method. Then you'll want to在Mixpanel类中注册并指定使用。
<?php //这是一个简单的消费者,只将事件写给//发送给客户。类myloggingConsumer扩展了computerStrategies_abstrateconsumer {public函数persist($ batch){echo“ ”;回声“将发送批次:\ n”;echo json_encode($ batch)。“ \ n”;回声“
”返回true;}} $ mp = new mixpanel(“ mixpanel_project_token”,array(//将消费者类的名称提供给mixpanel构造函数“消费者” => array => array(“ logger” =>“ myloggingConsumer”),//现在告诉Mixpanel实例使用您的类“消费者” =>“ logger”));//此事件将在 tag $ mp-> track(“ test_event”,array(“ color” =>“ blue”)中发送给客户端);
更新 almost 2 years ago