Trace event
For the first time, it is recommended to track 3 to 5 key events, and it only takes a few lines of code to experience the analysis function of Divine Analysis. For example:
Photo social products that track users browsing pictures and commenting on events E-commerce products can track events such as user registration, browsing products and placing orders
User use track_by_id() interface record events. For any event, it must containUser identifier(identity)、event name(event_name)two parameters. Meanwhile, user can track_by_id() the third parameter pass object object, Add custom event properties to events. Taking e-commerce products as an example, you can track a purchase like this:
<?php $identity = new SensorsAnalyticsIdentity(array('$identity_login_id' => '123')) ; $properties = array( # '$time' 属性是系统预置属性,传入毫秒表示的 Timestamp,表示事件发生的时间,如果不填入该属性,则默认使用系统当前时间 '$time' => (int)(microtime(true) * 1000), # 对于windows用户需要这样定义 '$time' => substr((microtime(true) * 1000), 0, 13) # '$ip' 属性是系统预置属性,如果服务端中能获取用户 IP 地址,并填入该属性,神策分析会自动根据 IP 地址解析用户的省份、城市信息 '$ip' => '123.123.123.123', # 商品 ID 'ProductId' => '123456', # 商品类别 'ProductCatalog' => 'Laptop Computer', # 是否加入收藏夹,Boolean 类型的属性 'IsAddedToFav' => true, ); # 记录用户浏览商品事件 $sa->track_by_id($identity, 'ViewProduct', $properties); $properties = array( # 用户 IP 地址 '$ip' => '123.123.123.123', # 商品 ID 列表,list<str> 类型的属性 'ProductIdList' => array('123456', '234567', '345678'), # 订单价格 'OrderPaid' => 12.10, ); # 记录用户订单付款事件 $sa->track_by_id($identity, 'PaidOrder', $properties); $sa->flush(); ?>
User identification
In the server-side application, the Distinct Id of the user is also required to be set for each event, which helps the analytics provide more accurate data such as retention rates.
For registered users, a user ID in the system is recommended as a Distinct Id. Modified information such as the user name, Email address, and mobile phone number are not recommended.
All of the track and profile family methods must be specified simultaneouslyuser ID 及Whether the user ID is the login ID these two parameters, in order to clearly inform the Sensors analytics of the type of user ID.
User registration/login
When the user logs in, call bind() bind, the first parameter is fromDetailed list of preset id keys.For example, bind a phone number to a user ("ABCDEF123456789") :
<?php $identity = new SensorsAnalyticsIdentity(array('$identity_login_id' => 'ABCDEF123456789','$identity_mobile' => '123')) ; $sa -> bind($identity); ?>
Called when multiple user ids are disassociated unbind() unbind, the first parameter is the key of the disassociation, and the second parameter is the ID of the corresponding disassociation user
<?php $identity = new SensorsAnalyticsIdentity(array('$identity_mobile' => '123')) ; $sa -> unbind($identity); ?>
Set user properties
In order to provide more accurate analysis services for the population, the Analytics SDK can set user attributes, such as age, gender, and so on. Users can use user attributes as filtering criteria or user attributes as dimensions for multi-dimensional analysis in retention analysis, distribution analysis and other functions.
Record user properties
Use profile_set_by_id() Set user properties:
<?php $identity = new SensorsAnalyticsIdentity(array('$identity_login_id' => 'iu001')); $properties = array( # 用户性别属性(Sex)为男性 'Sex' => 'Male', # 用户等级属性(Level)为 VIP 'UserLevel' => 'Elite VIP', ); # 设置用户属性 $sa->profile_set_by_id($identity, $properties); ?>
For user attributes that are no longer needed, you can use profile_unset_by_id()interface delete the attribute. In user attributes, the constraints on the attribute name and attribute value are the same as those on event attributes. For details, seeData format。
Records the properties that were first set
We can use properties that are only valid when first setprofile_set_once_by_id() record these attributes. Different from profile_set_by_id() interface, If the set user property already exists, the record is ignored without overwriting existing data, and is automatically created if the property does not exist. Therefore,profile_set_once_by_id() is applicable to setting the first activation time and first registration time. For example:
<?php $identity = new SensorsAnalyticsIdentity(array('$identity_login_id' => 'ABCDEF123456789')); # 设置用户渠道属性(AdSource)为 "App Store" $sa->profile_set_once_by_id($identity, array('AdSource' => 'App Store')); # 再次设置用户渠道属性(AdSource),设定无效,属性 "AdSource" 的值仍为 "App Store" $sa->profile_set_once_by_id($identity, array('AdSource' => 'Search Engine')); ?>
Numeric Type Attributes
For numeric user attributes, you can use the profile_increment() function to accumulate the attribute values. This is commonly used to record user payment frequency, payment amount, points, and other attributes. For example:
<?php $identity = new SensorsAnalyticsIdentity(array('$identity_login_id' => 'ABCDEF123456789')); # 设置用户游戏次数属性(GamePlayed),将次数累加1次 $sa->profile_increment_by_id($identity, array('GamePlayed' => 1)); ?>
List Type Attributes
For attributes such as movies that users like or restaurants that they have reviewed, you can record them as list type attributes. It should be noted that the elements in the list type attributes must be of the string type, and the values of the elements will be automatically deduplicated. For more information about the limitations of list type attributes, please refer to the Data format.
<?php $identity = new SensorsAnalyticsIdentity(array('$identity_login_id' => 'ABCDEF123456789')); // 传入properties,设置用户喜欢的电影属性(movies)和喜欢的游戏属性(games) // 设置成功后,"Movies" 属性值为 ["Sicario", "Love Letter"];"Games" 属性值为 ["Call of Duty", "Halo"] $sa->profile_append_by_id($identity, array('Movies' => array('Sicario','Love Letter'),'Games' => array('Call of Duty', 'Halo'))); // 传入属性名称和需要插入属性的值,设置用户喜欢的电影属性(Movies) // 设置成功后 "Movies" 属性值为 ["Sicario", "Love Letter", "Dead Poets Society"] $sa->profile_append_by_id($identity, array('Movies' => array('Dead Poets Society'))); // 传入属性名称和需要插入属性的值,设置用户喜欢的电影属性(Movies), // 但属性值 "Love Letter" 与已列表中已有元素重复,操作无效, // "Movies" 属性值仍然为 ["Sicario", "Love Letter", "Dead Poets Society"] $sa->profile_append_by_id($identity, array('Movies' => array('Love Letter'))); ?>