C# SDK Demo
|
Collect
Below is an example of using the C# SDK to send data to the SensaData Analysis System on a typical e-commerce product in the backend server.
It is important to note that this demo only describes SA's data recording capabilities and does not mean that users need to completely copy these designed events and properties. Users still need to design the corresponding Events and Properties based on their own product requirements and specific product forms.
// LoggingConsumer IConsumer consumer = new LoggingConsumer("D:/test/"); SensorsAnalytics sa = new SensorsAnalytics(consumer); //设置公共属性,以后上传的每一个事件都附带该属性 Dictionary<string, Object> properties = new Dictionary<string, object>(); // 服务器应用版本 properties.Add("ServerVersion", "1.2"); // 服务器机房地址 properties.Add("Location", "BeiJing"); // 设置事件公共属性 sa.RegisterSuperPropperties(properties); // 1. 用户匿名访问网站,cookieId 默认神策生成分配 String cookieId = "ABCDEF123456789"; // 1.1 访问首页 // 前面有$开头的property字段,是SA提供给用户的预置字段 // 对于预置字段,已经确定好了字段类型和字段的显示名 Dictionary<string, Object> firstRecord = new Dictionary<string, object>(); firstRecord.Add("$time", DateTime.Now); firstRecord.Add("Channel", "baidu"); firstRecord.Add("$project", "abc"); firstRecord.Add("$token", "123"); sa.Track(cookieId, "track", firstRecord); // 1.2 搜索商品 Dictionary<string, Object> searchRecord = new Dictionary<string, object>(); searchRecord.Add("KeyWord", "XX手机"); sa.Track(cookieId, "SearchProduct", searchRecord); // 1.3 浏览商品 Dictionary<string, Object> lookRecord = new Dictionary<string, object>(); lookRecord.Add("ProductName", "XX手机"); lookRecord.Add("ProductType", "智能手机"); lookRecord.Add("ShopName", "XX官方旗舰店"); sa.Track(cookieId, "ViewProduct", lookRecord); // 2. 用户注册登录之后,系统分配的注册ID // 注册用户的真实 ID String registerId = "0012345678"; // 用户注册/登录时,将用户注册 ID 与 匿名 ID 关联 sa.TrackSignUp(registerId, cookieId); // 2.2 用户注册时,填充了一些个人信息,可以用Profile接口记录下来 Dictionary<string, Object> userRecord = new Dictionary<string, object>(); userRecord.Add("$city", "武汉"); userRecord.Add("$province", "湖北"); userRecord.Add("$name", "昵称123"); userRecord.Add("$signup_time", DateTime.Now); userRecord.Add("Gender", "male"); userRecord.Add("age", 20); // $is_login_id 属性判断 distinct_id 是否为登录 ID,如果是则设置为 true,否则为 false,默认为 false userRecord.Add("$is_login_id", true); sa.ProfileSet(cookieId, userRecord); //2.3 设置首次访问时间 Dictionary<string, Object> firstVisitRecord = new Dictionary<string, object>(); firstVisitRecord.Add("$first_visit_time", DateTime.Now); // $is_login_id 属性判断 distinct_id 是否为登录 ID,如果是则设置为 true,否则为 false,默认为 false firstVisitRecord.Add("$is_login_id", true); sa.ProfileSetOnce(cookieId, firstVisitRecord); //2.4 追加属性 List<String> newInterest = new List<String>(); newInterest.Add("ball"); Dictionary<string, Object> appendRecord = new Dictionary<string, object>(); appendRecord.Add("interest", newInterest); sa.ProfileAppend(cookieId, appendRecord); //2.5 给属性加值 Dictionary<string, Object> incrementRecord = new Dictionary<string, object>(); incrementRecord.Add("age", 2); sa.ProfileIncrement(cookieId, incrementRecord); //2.6 移除用户属性 sa.ProfileUnset(cookieId, "age"); //物品纬度表上报 String itemId = "product001", itemType = "mobile"; Dictionary<string, Object> addRecord = new Dictionary<string, object>(); addRecord.Add("color", "white"); addRecord.Add("price", 31.54); sa.ItemSet(itemType, itemId, addRecord); //删除物品纬度信息 sa.ItemDelete(itemType, itemId);
SDK version needs to be >= v2.1.0
// LoggingConsumer IConsumer consumer = new LoggingConsumer("D:/test/"); SensorsAnalytics sa = new SensorsAnalytics(consumer); String distinctId = "ABCDEF123456789"; //设置公共属性,以后上传的每一个事件都附带该属性 Dictionary<string, Object> properties = new Dictionary<string, object>(); // 服务器应用版本 properties.Add("ServerVersion", "1.2"); // 服务器机房地址 properties.Add("Location", "BeiJing"); // 设置事件公共属性 sa.RegisterSuperPropperties(properties); // 1. 用户匿名访问网站,cookieId 默认神策生成分配 String cookieId = "ABCDEF123456789"; // 1.1 访问首页 // 前面有$开头的property字段,是SA提供给用户的预置字段 // 对于预置字段,已经确定好了字段类型和字段的显示名 Dictionary<string, Object> firstRecord = new Dictionary<string, object>(); firstRecord.Add("$time", DateTime.Now); firstRecord.Add("Channel", "baidu"); firstRecord.Add("$project", "abc"); firstRecord.Add("$token", "123"); SensorsAnalyticsIdentity identity = new SensorsAnalyticsIdentity("cookieId", cookieId); sa.TrackById(identity, "track", firstRecord); // 1.2 搜索商品 Dictionary<string, Object> searchRecord = new Dictionary<string, object>(); searchRecord.Add("KeyWord", "XX手机"); sa.TrackById(identity, "SearchProduct", searchRecord); // 1.3 浏览商品 Dictionary<string, Object> lookRecord = new Dictionary<string, object>(); lookRecord.Add("ProductName", "XX手机"); lookRecord.Add("ProductType", "智能手机"); lookRecord.Add("ShopName", "XX官方旗舰店"); sa.TrackById(identity, "ViewProduct", lookRecord); // 2. 用户注册登录之后,系统分配的注册ID // 匿名 ID 由前端传过来 String anonymousId = "9771C579-71F0-4650-8EE8-8999FA717761"; // 注册用户的真实 ID String registerId = "0012345678"; // 用户注册/登录时,将用户注册 ID 与 匿名 ID 关联 sa.TrackSignUp(registerId, anonymousId); // 2.2 用户注册时,填充了一些个人信息,可以用Profile接口记录下来 Dictionary<string, Object> userRecord = new Dictionary<string, object>(); userRecord.Add("$city", "武汉"); userRecord.Add("$province", "湖北"); userRecord.Add("$name", "昵称123"); userRecord.Add("$signup_time", DateTime.Now); userRecord.Add("Gender", "male"); userRecord.Add("age", 20); sa.ProfileSetById(identity, userRecord); //2.3 设置首次访问时间 Dictionary<string, Object> firstVisitRecord = new Dictionary<string, object>(); firstVisitRecord.Add("$first_visit_time", DateTime.Now); // $is_login_id 属性判断 distinct_id 是否为登录 ID,如果是则设置为 true,否则为 false,默认为 false firstVisitRecord.Add("$is_login_id", true); sa.ProfileSetOnceById(identity, firstVisitRecord); //2.4 追加属性 List<String> newInterest = new List<String>(); newInterest.Add("ball"); Dictionary<string, Object> appendRecord = new Dictionary<string, object>(); appendRecord.Add("interest", newInterest); sa.ProfileAppendById(identity, appendRecord); //2.5 给属性加值 Dictionary<string, Object> incrementRecord = new Dictionary<string, object>(); incrementRecord.Add("age", 2); sa.ProfileIncrementById(identity, incrementRecord); //2.6 移除用户属性 sa.ProfileUnsetById(identity, "age"); //物品纬度表上报 String itemId = "product001", itemType = "mobile"; Dictionary<string, Object> addRecord = new Dictionary<string, object>(); addRecord.Add("color", "white"); addRecord.Add("price", 31.54); sa.ItemSet(itemType, itemId, addRecord); //删除物品纬度信息 sa.ItemDelete(itemType, itemId);
Note: The content of this document is a technical document that provides details on how to use the Sensors product and does not include sales terms; the specific content of enterprise procurement products and technical services shall be subject to the commercial procurement contract.