Skip to content

Commit

Permalink
Merge pull request #355 from lsj8706/feat/#354-콕찌르기-Amplitude-트래킹
Browse files Browse the repository at this point in the history
[Feat] #354 - 콕 찌르기 Amplitude 이벤트 트래킹
  • Loading branch information
lsj8706 authored Jan 31, 2024
2 parents a038731 + 23dc252 commit 0c2bf96
Show file tree
Hide file tree
Showing 21 changed files with 322 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// AmplitudeEventPropertyBuilder.swift
// Core
//
// Created by sejin on 1/21/24.
// Copyright © 2024 SOPT-iOS. All rights reserved.
//

import Foundation

public class AmplitudeEventPropertyBuilder<Value: AmplitudeEventPropertyValueConvertible> {
private var eventProperties = [String: Any]()

public init() {}

/// ViewType 은 UserType과 같다.
public func addViewType() -> Self {
let key: AmplitudeEventPropertyKey = .viewType
let userType = UserDefaultKeyList.Auth.getUserType()
let value = userType.rawValue.lowercased()
self.eventProperties[key.rawValue] = value
return self
}

public func add(key: String, value: Any) -> Self {
self.eventProperties[key] = value
return self
}

public func add(key: AmplitudeEventPropertyKey, value: Any) -> Self {
self.eventProperties[key.rawValue] = value
return self
}

public func add(key: AmplitudeEventPropertyKey, value: Optional<Any>) -> Self {
self.eventProperties[key.rawValue] = value
return self
}

public func add(key: AmplitudeEventPropertyKey, value: Value) -> Self {
self.eventProperties[key.rawValue] = value.toString()
return self
}

public func removeOptional() -> Self {
self.eventProperties = self.eventProperties.compactMapValues { $0 }
return self
}

public func build() -> [String: Any] {
return eventProperties
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// AmplitudeEventPropertyKey.swift
// Core
//
// Created by sejin on 1/21/24.
// Copyright © 2024 SOPT-iOS. All rights reserved.
//

import Foundation

public enum AmplitudeEventPropertyKey: String {
case viewType = "view_type"
case clickViewType = "click_view_type"

// 콕 찌르기 피쳐 관련 Key
case viewProfile = "view_profile" // 멤버 프로필 id
case friendType = "friend_type"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// AmplitudeEventPropertyValueConvertible.swift
// Core
//
// Created by sejin on 1/21/24.
// Copyright © 2024 SOPT-iOS. All rights reserved.
//

import Foundation

public protocol AmplitudeEventPropertyValueConvertible {
func toString() -> String
}
39 changes: 39 additions & 0 deletions SOPT-iOS/Projects/Core/Sources/Amplitude/AmplitudeEventType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// AmplitudeEventType.swift
// Core
//
// Created by sejin on 2023/09/21.
// Copyright © 2023 SOPT-iOS. All rights reserved.
//

import Foundation

public enum AmplitudeEventType: String {
// 클릭 이벤트
case clickAlarm = "click_alarm"
case clickMyPage = "click_mypage"
case clickAttendacne = "click_attendance"
case clickGroup = "click_group"
case clickProject = "click_project"
case clickMember = "click_member"
case clickOfficialHomepage = "click_homepage"
case clickSoptamp = "click_soptamp"
case clickInstagram = "click_instagram"
case clickYoutube = "click_youtube"
case clickReview = "click_review"
case clickFaq = "click_faq"
case clickPlaygroundCommunity = "click_playground_community"
case clickPoke = "click_poke"
case clickMemberProfile = "click_memberprofile"
case clickPokeIcon = "click_poke_icon"
case clickPokeAlarmDetail = "click_poke_alarm_detail"
case clickPokeQuit = "click_poke_quit"

// 뷰 이벤트
case viewAppHome = "view_apphome"
case viewPokeOnboarding = "view_poke_onboarding"
case viewPokeMain = "view_poke_main"
case viewPokeAlarmDetail = "view_poke_alarm_detail"
case viewPokeFriend = "view_poke_friend"
case viewPokeFriendDetail = "view_poke_friend_detail"
}
23 changes: 12 additions & 11 deletions SOPT-iOS/Projects/Core/Sources/Amplitude/AmplitudeInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,25 @@ public struct AmplitudeInstance {
}

public extension Amplitude {
func track(event: AmplitudeEventType, userType: UserType, otherProperties: [String: Any]? = nil) {
let eventType: String = event.rawValue
var eventProperties: [String: Any] = ["view_type": userType.rawValue.lowercased()]

if let otherProperties = otherProperties {
for (key, value) in otherProperties {
eventProperties.updateValue(value, forKey: key)
}
}
func track(eventType: AmplitudeEventType, eventProperties: [String: Any]? = nil) {
let eventType: String = eventType.rawValue

AmplitudeInstance.shared.track(eventType: eventType, eventProperties: eventProperties, options: nil)
}

func trackWithUserType(event: AmplitudeEventType) {
func trackWithUserType(event: AmplitudeEventType, otherProperties: [String: Any]? = nil) {
let eventType: String = event.rawValue
let userType = UserDefaultKeyList.Auth.getUserType()
var eventProperties: [String: Any] = ["view_type": userType.rawValue.lowercased()]
let eventProperties: [String: Any] = [AmplitudeEventPropertyKey.viewType.rawValue: userType.rawValue.lowercased()]

AmplitudeInstance.shared.track(eventType: eventType, eventProperties: eventProperties, options: nil)
}

func addPushNotificationAuthorizationIdentity(isAuthorized: Bool) {
let identify = Identify()
let key: AmplitudeUserPropertyKey = .stateOfPushNotification
identify.set(property: key.rawValue, value: isAuthorized)

AmplitudeInstance.shared.identify(identify: identify)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// AmplitudeUserPropertyKey.swift
// Core
//
// Created by sejin on 1/21/24.
// Copyright © 2024 SOPT-iOS. All rights reserved.
//

import Foundation

public enum AmplitudeUserPropertyKey: String {
case stateOfPushNotification = "state_of_push_notification"
}
28 changes: 0 additions & 28 deletions SOPT-iOS/Projects/Core/Sources/Enum/AmplitudeEventType.swift

This file was deleted.

24 changes: 12 additions & 12 deletions SOPT-iOS/Projects/Core/Sources/Enum/ServiceTypeTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ import Foundation
public extension ServiceType {
var toAmplitudeEventType: AmplitudeEventType {
switch self {
case .officialHomepage: return .officialHomepage
case .review: return .review
case .project: return .project
case .faq: return .faq
case .youtube: return .youtube
case .attendance: return .attendacne
case .member: return .member
case .group: return .group
case .instagram: return .instagram
case .playgroundCommunity: return .playgroundCommunity
case .officialHomepage: return .clickOfficialHomepage
case .review: return .clickReview
case .project: return .clickProject
case .faq: return .clickFaq
case .youtube: return .clickYoutube
case .attendance: return .clickAttendacne
case .member: return .clickMember
case .group: return .clickGroup
case .instagram: return .clickInstagram
case .playgroundCommunity: return .clickPlaygroundCommunity
}
}
}

public extension AppServiceType {
var toAmplitudeEventType: AmplitudeEventType {
switch self {
case .soptamp: return .soptamp
case .poke: return .poke
case .soptamp: return .clickSoptamp
case .poke: return .clickPoke
}
}
}
16 changes: 0 additions & 16 deletions SOPT-iOS/Projects/Core/Sources/Utils/trackAmplitudeEvent.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extension AppLifecycleAdapter {
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] _ in
self?.reissureTokens()
self?.checkNotificationSetting()
}).store(in: self.cancelBag)
}

Expand All @@ -44,4 +45,11 @@ extension AppLifecycleAdapter {

self.authService.reissuance { _ in }
}

private func checkNotificationSetting() {
UNUserNotificationCenter.current().getNotificationSettings { setting in
let isNotificationAuthorized = setting.authorizationStatus == .authorized
AmplitudeInstance.shared.addPushNotificationAuthorizationIdentity(isAuthorized: isNotificationAuthorized)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ extension MainViewModel {
.sink { [weak self] _ in
guard let self = self else { return }
self.onNoticeButtonTap?()
self.trackAmplitude(event: .alarm)
self.trackAmplitude(event: .clickAlarm)
}.store(in: cancelBag)

input.myPageButtonTapped
.sink { [weak self] _ in
guard let self = self else { return }
self.onMyPageButtonTap?(self.userType)
self.trackAmplitude(event: .myPage)
self.trackAmplitude(event: .clickMyPage)
}.store(in: cancelBag)

input.cellTapped
Expand Down Expand Up @@ -147,7 +147,7 @@ extension MainViewModel {
guard let self = self else { return }
self.requestAuthorizationForNotification()
self.useCase.getServiceState()
self.trackAmplitude(event: .main)
self.trackAmplitude(event: .viewAppHome)
}.store(in: cancelBag)

return output
Expand Down Expand Up @@ -235,10 +235,8 @@ extension MainViewModel {
// APNS 권한 허용 확인
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { granted, error in
if let error = error {
print(error)
}

if let error = error { print(error) }
AmplitudeInstance.shared.addPushNotificationAuthorizationIdentity(isAuthorized: granted)
print("APNs-알림 권한 허용 유무 \(granted)")

if granted {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,17 @@ public enum PokeRelation: String {
case soulmate = "천생연분"
}


extension PokeRelation {
public var toEnglishName: String {
switch self {
case .nonFriend:
return "nonFriend"
case .newFriend:
return "newFriend"
case .bestFriend:
return "bestFriend"
case .soulmate:
return "soulmate"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// PokeAmplitudeEventPropertyValue.swift
// PokeFeature
//
// Created by sejin on 1/21/24.
// Copyright © 2024 SOPT-iOS. All rights reserved.
//

import Foundation
import Core

enum PokeAmplitudeEventPropertyValue: String, AmplitudeEventPropertyValueConvertible {
case onboarding = "onboarding"
case pokeMainAlarm = "poke_main_alarm"
case pokeMainFriend = "poke_main_friend"
case pokeMainRecommendNotMyFriend = "poke_main_recommend_notmyfriend"
case pokeMainRecommendMyFriend = "poke_main_recommend_myfriend"
case pokeAlarm = "poke_alarm"
case friend = "friend"

func toString() -> String {
self.rawValue
}
}
Loading

0 comments on commit 0c2bf96

Please sign in to comment.