add user event

This commit is contained in:
anlicheng 2025-09-10 16:40:33 +08:00
parent c84ee6d630
commit 24f31805da
7 changed files with 85 additions and 1 deletions

View File

@ -37,6 +37,53 @@ struct API {
// //
static let baseUrl = "https://dimensionhub.s5s8.com" static let baseUrl = "https://dimensionhub.s5s8.com"
// token
static func appInit<T: Codable>(userId: String, as: T.Type) async -> APIResponse<T> {
// Create the request
var request = URLRequest(url: URL(string: baseUrl + "/api/appInit")!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
// Convert parameters to JSON data
let device = await UIDevice.current
let system = "\(await device.systemName):\(await device.systemVersion)"
let parameters: [String:Any] = [
"user_id": userId,
"system": system,
"lang": NSLocale.preferredLanguages.first ?? "",
"vendorId": await UIDevice.current.identifierForVendor?.uuidString ?? ""
]
let jsonData = try! JSONSerialization.data(withJSONObject: parameters, options: [])
request.httpBody = jsonData
let str = String(data: jsonData, encoding: .utf8)!
print("json is: \(str)")
return await doRequest(request: request, as: T.self)
}
static func userEvent<T: Codable>(userId: String, event: String, as: T.Type) async -> APIResponse<T> {
// Create the request
var request = URLRequest(url: URL(string: baseUrl + "/api/appInit")!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
// Convert parameters to JSON data
let parameters: [String:Any] = [
"user_id": userId,
"event": event
]
let jsonData = try! JSONSerialization.data(withJSONObject: parameters, options: [])
request.httpBody = jsonData
let str = String(data: jsonData, encoding: .utf8)!
print("json is: \(str)")
return await doRequest(request: request, as: T.self)
}
// token // token
static func sendDeviceTokenToServer<T: Codable>(userId: String, token: String, as: T.Type) async -> APIResponse<T> { static func sendDeviceTokenToServer<T: Codable>(userId: String, token: String, as: T.Type) async -> APIResponse<T> {
// Create the request // Create the request

View File

@ -64,6 +64,7 @@ struct DetailView: View {
FollowButtonView(dramaStatus: status) { followStatus in FollowButtonView(dramaStatus: status) { followStatus in
Task.detached { Task.detached {
await self.clickFollowButton(followStatus: followStatus) await self.clickFollowButton(followStatus: followStatus)
_ = await API.userEvent(userId: self.userId, event: "click_follow_button", as: String.self)
} }
} }
} }
@ -120,6 +121,7 @@ struct DetailView: View {
} }
.task { .task {
await detailModel.loadData(userId: self.userId, id: self.id, channelName: self.channelName) await detailModel.loadData(userId: self.userId, id: self.id, channelName: self.channelName)
_ = await API.userEvent(userId: self.userId, event: "view_detail:\(self.id)", as: String.self)
} }
} }

View File

@ -41,6 +41,7 @@ struct FollowListView: View {
.frame(width: 370) .frame(width: 370)
.task { .task {
self.num = await self.followModel.loadData(userId: self.userId) self.num = await self.followModel.loadData(userId: self.userId)
_ = await API.userEvent(userId: self.userId, event: "view_follow_list", as: String.self)
} }
} }
} }

View File

@ -57,6 +57,9 @@ struct IndexMainView: View {
.highPriorityGesture( .highPriorityGesture(
TapGesture().onEnded { TapGesture().onEnded {
appNavigation.append(dest: .followList(num: Int(indexModel.follow_num) ?? 0)) appNavigation.append(dest: .followList(num: Int(indexModel.follow_num) ?? 0))
Task {
_ = await API.userEvent(userId: self.userId, event: "click_follow_list", as: String.self)
}
} }
) )
.zIndex(1) .zIndex(1)
@ -136,6 +139,8 @@ struct IndexMainView: View {
let loadNum = await self.indexModel.loadPrevUpdateDramasTask(userId: self.userId) let loadNum = await self.indexModel.loadPrevUpdateDramasTask(userId: self.userId)
self.hasMoreNewest = loadNum > 3 self.hasMoreNewest = loadNum > 3
_ = await API.userEvent(userId: self.userId, event: "load_prev", as: String.self)
} }
} }
.onChange(of: scrollID) { _, newValue in .onChange(of: scrollID) { _, newValue in
@ -188,8 +193,13 @@ struct IndexMainView: View {
let num = await indexModel.loadMoreUpdateDramasTask(userId: self.userId) let num = await indexModel.loadMoreUpdateDramasTask(userId: self.userId)
self.hasMoreOldest = num > 3 self.hasMoreOldest = num > 3
_ = await API.userEvent(userId: self.userId, event: "load_more", as: String.self)
} }
} }
.task {
_ = await API.userEvent(userId: self.userId, event: "view_index", as: String.self)
}
.onAppear { .onAppear {
Task { @MainActor in Task { @MainActor in
await self.indexModel.loadData(userId: self.userId) await self.indexModel.loadData(userId: self.userId)
@ -234,6 +244,7 @@ extension IndexMainView {
struct MixGroupLabelView: View { struct MixGroupLabelView: View {
@EnvironmentObject var appNav: AppNavigation @EnvironmentObject var appNav: AppNavigation
@Environment(\.userId) private var userId
let fixedDramaGroup: IndexModel.UpdateDramaGroup? let fixedDramaGroup: IndexModel.UpdateDramaGroup?
let onTap: () -> Void let onTap: () -> Void
@ -242,6 +253,9 @@ extension IndexMainView {
HStack(alignment: .center) { HStack(alignment: .center) {
Button(action: { Button(action: {
appNav.append(dest: .search) appNav.append(dest: .search)
Task {
_ = await API.userEvent(userId: self.userId, event: "click_search_button", as: String.self)
}
}) { }) {
Image(systemName: "magnifyingglass") Image(systemName: "magnifyingglass")
.font(.system(size: 20)) .font(.system(size: 20))

View File

@ -51,6 +51,7 @@ struct ListView: View {
.navigationTitle(detailModel.name) .navigationTitle(detailModel.name)
.task { .task {
await detailModel.loadData(userId: self.userId, id: self.id, selectedChannelIdx: self.selectedChannelIdx) await detailModel.loadData(userId: self.userId, id: self.id, selectedChannelIdx: self.selectedChannelIdx)
_ = await API.userEvent(userId: self.userId, event: "view_list:\(self.id)", as: String.self)
} }
} }

View File

@ -65,6 +65,9 @@ struct SearchView: View {
ToolbarItem(placement: .principal) { ToolbarItem(placement: .principal) {
SearchBar(isFirstResponder: $isFocused, searchText: $searchText) { SearchBar(isFirstResponder: $isFocused, searchText: $searchText) {
doSearch() doSearch()
Task {
_ = await API.userEvent(userId: self.userId, event: "search:\(self.searchText)", as: String.self)
}
} }
.frame(width: UIScreen.main.bounds.width * 0.8) .frame(width: UIScreen.main.bounds.width * 0.8)
.id(toolbarID) .id(toolbarID)

View File

@ -79,12 +79,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
self.handleAppRefresh(task: task) self.handleAppRefresh(task: task)
} }
let userId = KeychainHelper.getPersistentUserId()
self.appInit(userId: userId)
Task.detached { Task.detached {
await self.registerForPushNotifications() await self.registerForPushNotifications()
// //
let unreadDramaIds = await self.getSystemUnreadDramaIds() let unreadDramaIds = await self.getSystemUnreadDramaIds()
let userId = KeychainHelper.getPersistentUserId()
let response = await API.resetUserUnreadNum(userId: userId, dramaIds: unreadDramaIds, as: String.self) let response = await API.resetUserUnreadNum(userId: userId, dramaIds: unreadDramaIds, as: String.self)
switch response { switch response {
case .result(let result): case .result(let result):
@ -102,9 +105,22 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
} }
} }
} }
return true return true
} }
private func appInit(userId: String) {
Task {
let response = await API.appInit(userId: userId, as: String.self)
switch response {
case .result(let result):
NSLog("reset user unread num result is: \(result)")
case .error(let errorCode, let message):
NSLog("reset user unread error_code: \(errorCode), message: \(message)")
}
}
}
private func handleAppRefresh(task: BGTask) { private func handleAppRefresh(task: BGTask) {
self.scheduleAppRefresh() self.scheduleAppRefresh()