Browse Source

实现用户行为记录功能

pull/19/head
Trae AI 1 month ago
parent
commit
a91df109f4
  1. 31
      app.js
  2. 18
      pages/goods-detail/goods-detail.js
  3. 9
      pages/index/index.js
  4. 1
      project.config.json
  5. 2
      project.private.config.json

31
app.js

@ -1,8 +1,11 @@
// app.js
const api = require('./utils/api');
App({
globalData: {
userInfo: null,
currentTab: 'index'
currentTab: 'index',
sessionStartTime: null
},
// 事件总线,用于页面间通信
@ -33,15 +36,41 @@ App({
this.globalData.currentTab = tabName;
},
// 记录用户踪迹
recordUserTrace(action, additionalData = {}) {
const traceData = {
action: action,
timestamp: new Date().toISOString(),
...additionalData
};
api.addUserTrace(traceData);
},
onLaunch() {
console.log('App launched');
// 记录用户进入小程序
this.recordUserTrace('app_launch');
this.globalData.sessionStartTime = new Date().toISOString();
},
onShow() {
console.log('App shown');
// 记录用户显示小程序
this.recordUserTrace('app_show');
if (!this.globalData.sessionStartTime) {
this.globalData.sessionStartTime = new Date().toISOString();
}
},
onHide() {
console.log('App hidden');
// 记录用户隐藏小程序,包含会话时长
const sessionEndTime = new Date().toISOString();
this.recordUserTrace('app_hide', {
sessionStartTime: this.globalData.sessionStartTime,
sessionEndTime: sessionEndTime,
sessionDuration: new Date(sessionEndTime) - new Date(this.globalData.sessionStartTime)
});
this.globalData.sessionStartTime = null;
}
});

18
pages/goods-detail/goods-detail.js

@ -1613,6 +1613,15 @@ Page({
return;
}
// 记录用户收藏行为
API.addUserTrace({
action: 'add_favorite',
productId: productId,
productName: this.data.goodsDetail.name || ''
}).catch(err => {
console.error('记录收藏踪迹失败:', err);
});
wx.showLoading({ title: '正在收藏...' });
// 调用API添加收藏
@ -1673,6 +1682,15 @@ Page({
return;
}
// 记录用户取消收藏行为
API.addUserTrace({
action: 'cancel_favorite',
productId: productId,
productName: this.data.goodsDetail.name || ''
}).catch(err => {
console.error('记录取消收藏踪迹失败:', err);
});
wx.showLoading({ title: '正在取消收藏...' });
// 调用API取消收藏

9
pages/index/index.js

@ -1980,6 +1980,15 @@ Page({
console.log('searchGoods - 开始搜索,关键词:', this.data.searchKeyword);
console.log('searchGoods - 本地已有商品数量:', this.data.goods.length);
// 记录用户搜索行为
API.addUserTrace({
action: 'search',
keyword: this.data.searchKeyword,
region: this.data.selectedRegion
}).catch(err => {
console.error('记录搜索踪迹失败:', err);
});
// 先对本地已加载的商品进行过滤
const filteredGoods = this.applyFilters(this.data.goods, true);

1
project.config.json

@ -18,6 +18,7 @@
"http://localhost:3000",
"http://8.137.125.67:3000",
"http://localhost:3001",
"http://localhost:3003",
"https://youniao.icu"
],
"compileWorklet": false,

2
project.private.config.json

@ -1,6 +1,6 @@
{
"libVersion": "3.10.3",
"projectname": "Mini-Program",
"projectname": "xcx22",
"setting": {
"urlCheck": false,
"coverView": true,

Loading…
Cancel
Save