Menu

Easy User Association API (IDM2.0-PHP)

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:

Picture 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() interface record events. For any event, it must containUser identifierdistinct_id)、Whether the user ID is login ID (is_login_id) Andevent nameevent_name)three parameters. Meanwhile, user can use track() of the third argument is passed in one object object, add custom event properties to events. Taking e-commerce products as an example, you can track a purchase like this:

<?php 	$distinct_id = 'ABCDEF123456789'; 	$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($distinct_id, true, 'ViewProduct', $properties); 	$properties = array( 		# 用户 IP 地址 		'$ip' => '123.123.123.123', 		# 商品 ID 列表,list<str> 类型的属性 		'ProductIdList' => array('123456', '234567', '345678'), 		# 订单价格 		'OrderPaid' => 12.10, 	); 	# 记录用户订单付款事件 	$sa->track($distinct_id, true, '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 andUser ID Specifies whether the user ID is a login ID  two parameters are in order to explicitly inform the Shenze analysis of the type of user ID.

User registration/login

When the Distinct Id of the same user changes (usually anonymous user registration behavior) , you can usetrack_signup() associate the old Distinct Id with the new Distinct Id to ensure the accuracy of user analysis. For example:

<?php 	$anonymous_id = '9771C579-71F0-4650-8EE8-8999FA717761';# 匿名 ID 由前端传过来 	$register_id = '0012345678'; 	# 用户注册/登录时,将用户注册 ID 与 匿名 ID 关联 	$sa->track_signup($register_id, $anonymous_id); ?>


Note that for the same user,track_signup() In general, it is recommended to call only once (usually at the user register call), user login It is recommended to implement the association of the preceding and following behaviors on the business side. Called multiple times before the 1.13 version of Sensors Analysis track_signup() , only the first association behavior is valid. The method for multi-device id association was provided after the 1.13 version of Sensors Analytics. Please refer to more detailed instructions 如何正确地标识用户And contact our technical support staff if necessary.

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() set user properties:

<?php 	$distinct_id = 'ABCDEF123456789'; 	$properties = array( 		# 用户性别属性(Sex)为男性 		'Sex' => 'Male', 		# 用户等级属性(Level)为 VIP 	'UserLevel' => 'Elite VIP', ); 	# 设置用户属性 	$sa->profile_set($distinct_id, true, $properties); ?>

For user attributes that are no longer needed, you can delete them through the profile_unset() interface. In user attributes, the constraints on the attribute name and attribute value are the same as those on event attributes. For details, see Data format

Records the properties that were first set

We can use properties that are only valid when first set profile_set_once() to record these attributes. Different from profile_set() 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() is applicable to setting the first activation time and first registration time. For example:

<?php 	$distinct_id = 'ABCDEF123456789'; 	# 设置用户渠道属性(AdSource)为 "App Store" 	$sa->profile_set_once($distinct_id, true, array('AdSource' => 'App Store')); 	# 再次设置用户渠道属性(AdSource),设定无效,属性 "AdSource" 的值仍为 "App Store" 	$sa->profile_set_once($distinct_id, true, array('AdSource' => 'Search Engine')); ?>

Numerical type attribute

For numerical user attributes, you can use profile_increment() to accumulate the attribute value. This is commonly used to record attributes such as the number of payments made by a user, the total payment amount, and the user's points. For example:

<?php 	$distinct_id = 'ABCDEF123456789'; 	# 设置用户游戏次数属性(GamePlayed),将次数累加1次 	$sa->profile_increment($distinct_id, true, array('GamePlayed' => 1)); ?>

List type attribute

For attributes like a user's favorite movies or restaurants they have reviewed, you can record them as a list type attribute. Please note that the elements in the list type attribute must be of type string, and the values of the elements will be automatically deduplicated. For more information on the limitations of list types, please refer to Data format.

<?php 	$distinct_id = 'ABCDEF123456789'; 	$properties = array( 		# 电影列表 		'Movies' => array('Sicario', 'Love Letter'), 		# 游戏列表 		'Games' => array('Call of Duty', 'Halo'), 	); 	# 传入properties,设置用户喜欢的电影属性(movies)和喜欢的游戏属性(games) 	# 设置成功后,"Movies" 属性值为 ["Sicario", "Love Letter"];"Games" 属性值为 ["Call of Duty", "Halo"] 	$sa->profile_append($distinct_id, true, $properties); 	# 传入属性名称和需要插入属性的值,设置用户喜欢的电影属性(Movies) 	# 设置成功后 "Movies" 属性值为 ["Sicario", "Love Letter", "Dead Poets Society"] 	$sa->profile_append($distinct_id, true, array('Movie' => array('Dead Poets Society'))); 	# 传入属性名称和需要插入属性的值,设置用户喜欢的电影属性(Movies), 	# 但属性值 "Love Letter" 与已列表中已有元素重复,操作无效, 	# "Movies" 属性值仍然为 ["Sicario", "Love Letter", "Dead Poets Society"] 	$sa->profile_append($distinct_id, true, array('Movie' => array('Love Letter'))); ?>
Previous
Global User Association API (IDM3.0-PHP)
Next
Python SDK
Last modified: 2024-12-27