-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdemo.c
263 lines (207 loc) · 8.92 KB
/
demo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* Copyright (C) 2015 SensorsData
* All rights reserved.
*/
#include <string.h>
#include "sensors_analytics.h"
int main(int args, char** argv) {
(void)(args);
(void)(argv);
SALoggingConsumer* consumer = NULL;
if (SA_OK != sa_init_logging_consumer("demo.out", &consumer)) {
fprintf(stderr, "Failed to initialize the consumer.");
return 1;
}
SensorsAnalytics *sa = NULL;
if (SA_OK != sa_init(consumer, &sa)) {
fprintf(stderr, "Failed to initialize the SDK.");
return 1;
}
SAProperties* properties = NULL;
// 在这个 Demo 中,我们以一个典型的电商产品为例,描述一个用户从匿名访问网站,到下单购买商品,再到申请售后服务,
// 这样一个整个环节,使用 Sensors Analytics(以下简称 SA)的产品,应该如何记录日志。
// 特别需要注意的是,这个 Demo 只是描述 SA 的数据记录能力,并不是说使用者要完全照搬这些 Event 和 Property
// 的设计,使用者还是需要结合自己产品的实际需要,来做相应的设计和规划
// 1. 用户匿名访问网站.
const char* cookie_id = "ABCDEF123456789"; // 用户未登录时,可以使用产品自己生成的cookieId来标注用户.
// 1.1 访问首页
// 前面有$开头的property字段,是SA提供给用户的预置字段
// 对于预置字段,已经确定好了字段类型和字段的显示名.
{
properties = sa_init_properties();
if (NULL == properties) {
fprintf(stderr, "Failed to initialize the properties.");
return 1;
}
// 通过请求中的UA,可以解析出用户使用设备的操作系统是 iOS 的.
SA_ASSERT(SA_OK == sa_add_string("$os", "iOS", strlen("iOS"), properties));
// 操作系统的具体版本.
SA_ASSERT(SA_OK == sa_add_string("$os_version", "10.0.0", strlen("10.0.0"), properties));
// 请求中能够拿到用户的IP,则把这个传递给SA,SA会自动根据这个解析省份、城市.
SA_ASSERT(SA_OK == sa_add_string("$ip", "123.123.123.123", strlen("123.123.123.123"), properties));
// 是否首次访问.
SA_ASSERT(SA_OK == sa_add_bool("is_first_time", SA_FALSE, properties));
// 记录访问首页事件.
SA_ASSERT(SA_OK == sa_track(cookie_id, "ViewHomePage", properties, sa));
sa_free_properties(properties);
}
// 1.2 记录用户首次登陆时间.
{
properties = sa_init_properties();
if (NULL == properties) {
fprintf(stderr, "Failed to initialize the properties.");
return 1;
}
// 用户首次使用时间.
SA_ASSERT(SA_OK == sa_add_date("first_time", time(NULL), 0, properties));
// 记录用户属性.
SA_ASSERT(SA_OK == sa_profile_set_once(cookie_id, properties, sa));
sa_free_properties(properties);
}
// 1.3 搜索商品.
{
properties = sa_init_properties();
if (NULL == properties) {
fprintf(stderr, "Failed to initialize the properties.");
return 1;
}
// 通过请求中的UA,可以解析出用户使用设备的操作系统是 iOS 的.
SA_ASSERT(SA_OK == sa_add_string("$os", "iOS", strlen("iOS"), properties));
// 操作系统的具体版本.
SA_ASSERT(SA_OK == sa_add_string("$os_version", "10.0.0", strlen("10.0.0"), properties));
// 请求中能够拿到用户的IP,则把这个传递给SA,SA会自动根据这个解析省份、城市.
SA_ASSERT(SA_OK == sa_add_string("$ip", "123.123.123.123", strlen("123.123.123.123"), properties));
// 搜索引擎引流过来时使用的关键词.
SA_ASSERT(SA_OK == sa_add_string("key_word", "XX手机", 8, properties));
// 记录搜索商品事件.
SA_ASSERT(SA_OK == sa_track(cookie_id, "SearchProduct", properties, sa));
sa_free_properties(properties);
}
// 1.4 浏览商品.
{
properties = sa_init_properties();
if (NULL == properties) {
fprintf(stderr, "Failed to initialize the properties.");
return 1;
}
// 通过请求中的UA,可以解析出用户使用设备的操作系统是 iOS 的.
SA_ASSERT(SA_OK == sa_add_string("$os", "iOS", strlen("iOS"), properties));
// 操作系统的具体版本.
SA_ASSERT(SA_OK == sa_add_string("$os_version", "10.0.0", strlen("10.0.0"), properties));
// 请求中能够拿到用户的IP,则把这个传递给SA,SA会自动根据这个解析省份、城市.
SA_ASSERT(SA_OK == sa_add_string("$ip", "123.123.123.123", strlen("123.123.123.123"), properties));
// 商品名称.
SA_ASSERT(SA_OK == sa_add_string("product_name", "XX手机", 8, properties));
// 商品Tag.
SA_ASSERT(SA_OK == sa_append_list("product_tag", "大屏", 6, properties));
SA_ASSERT(SA_OK == sa_append_list("product_tag", "双卡双待", 12, properties));
// 商品价格.
SA_ASSERT(SA_OK == sa_add_number("product_price", 5888, properties));
// 记录浏览商品事件.
SA_ASSERT(SA_OK == sa_track(cookie_id, "ViewProduct", properties, sa));
sa_free_properties(properties);
}
// 2. 用户注册,注册后的注册 ID.
const char* login_id = "123456";
// 2.1 通过,track_signup,把匿名ID和注册ID关联起来.
{
properties = sa_init_properties();
if (NULL == properties) {
fprintf(stderr, "Failed to initialize the properties.");
return 1;
}
// 用户的注册渠道.
SA_ASSERT(SA_OK == sa_add_string("register", "Baidu", 5, properties));
// 关联注册用户与匿名用户.
SA_ASSERT(SA_OK == sa_track_signup(login_id, cookie_id, properties, sa));
sa_free_properties(properties);
}
// 2.2 用户注册时,填充了一些个人信息,可以用Profile接口记录下来.
{
properties = sa_init_properties();
if (NULL == properties) {
fprintf(stderr, "Failed to initialize the properties.");
return 1;
}
// 用户的注册渠道.
SA_ASSERT(SA_OK == sa_add_string("register", "Baidu", 5, properties));
// 用户注册日期.
SA_ASSERT(SA_OK == sa_add_date("$signup_time", time(NULL), 0, properties));
// 用户是否购买过商品.
SA_ASSERT(SA_OK == sa_add_bool("is_vip", SA_FALSE, properties));
// 设置用户属性.
SA_ASSERT(SA_OK == sa_profile_set(login_id, properties, sa));
sa_free_properties(properties);
}
// 3. 用户提交订单.
// 3.1 记录用户提交订单事件.
{
properties = sa_init_properties();
if (NULL == properties) {
fprintf(stderr, "Failed to initialize the properties.");
return 1;
}
// 通过请求中的UA,可以解析出用户使用设备的操作系统是 iOS 的.
SA_ASSERT(SA_OK == sa_add_string("$os", "iOS", strlen("iOS"), properties));
// 操作系统的具体版本.
SA_ASSERT(SA_OK == sa_add_string("$os_version", "10.0.0", strlen("10.0.0"), properties));
// 请求中能够拿到用户的IP,则把这个传递给SA,SA会自动根据这个解析省份、城市.
SA_ASSERT(SA_OK == sa_add_string("$ip", "123.123.123.123", strlen("123.123.123.123"), properties));
// 商品名称.
SA_ASSERT(SA_OK == sa_add_string("product_name", "XX手机", 8, properties));
// 商品价格.
SA_ASSERT(SA_OK == sa_add_int("product_price", 5888, properties));
// 商品折扣.
SA_ASSERT(SA_OK == sa_add_number("product_discount", 0.8, properties));
// 记录外部项目名称
SA_ASSERT(SA_OK == sa_add_string("$project", "MyOrder", strlen("MyOrder"), properties));
// 记录购买商品事件.
SA_ASSERT(SA_OK == sa_track(cookie_id, "SubmitOrder", properties, sa));
sa_free_properties(properties);
}
// 3.2 在用户属性中记录用户支付金额.
{
properties = sa_init_properties();
if (NULL == properties) {
fprintf(stderr, "Failed to initialize the properties.");
return 1;
}
// 累加用户支付金额.
SA_ASSERT(SA_OK == sa_add_number("pay", 5888, properties));
// 用户获得头衔.
SA_ASSERT(SA_OK == sa_append_list("title", "VIP", 3, properties));
// 记录搜索商品事件.
SA_ASSERT(SA_OK == sa_profile_increment(login_id, properties, sa));
sa_free_properties(properties);
}
// 3.3 在用户属性中增加用户头衔.
{
properties = sa_init_properties();
if (NULL == properties) {
fprintf(stderr, "Failed to initialize the properties.");
return 1;
}
// 用户获得头衔.
SA_ASSERT(SA_OK == sa_append_list("title", "VIP", 3, properties));
// 添加用户属性.
SA_ASSERT(SA_OK == sa_profile_append(login_id, properties, sa));
sa_free_properties(properties);
}
// 4. 其他.
// 4.1 删除用户某个属性.
{
SA_ASSERT(SA_OK == sa_profile_unset(login_id, "title", sa));
}
// 4.2 删除某个用户的所有属性.
{
SA_ASSERT(SA_OK == sa_profile_delete(login_id, sa));
}
// 4.3 不合法的事件名称.
{
SA_ASSERT(SA_INVALID_PARAMETER_ERROR == sa_track(login_id, "time", NULL, sa));
SA_ASSERT(SA_INVALID_PARAMETER_ERROR == sa_track(login_id, "100vip", NULL, sa));
}
sa_flush(sa);
sa_free(sa);
return 0;
}