ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • iOS Amplitude
    iOS 2022. 6. 23. 11:34

    안녕하세요!

    오늘은 Amplitude에 대해서 정리해보았습니다.

    바로 시작하겠습니다 🐶


     

    Amplitude란?

    Amplitude는 프로덕트 분석 툴로, 웹과 앱에 걸쳐 사용자들이 수행한 이벤트와 이벤트의 프로퍼티, 그리고 유저 프로퍼티의 통합 수집 기능을 제공한다.

    만약 Tunes라는 모바일 앱이 있다고 했을 때,

    다음과 같은 화면에서 사용자들은 노래를 실행하거나, 스킵하거나, 무작위 곡을 틀 수 있고, 공유도 할 수 있다.

    Amplitude를 사용하면 사용자들이 실행한 이런 행동을 추적할 수 있게 된다.

     

    기본적으로 이벤트 프로퍼티와 유저 프로퍼티를 설정할 수 있는데,

    이벤트 프로퍼티에는 어떤 이벤트인지, 그 이벤트의 속성은 무엇인지 설정해줄 수 있다.

    유저 프로퍼티는 IDFV 아이디를 기반으로 사용자를 구분하여 속성을 설정할 수 있게 해준다.

     

    인스턴스 생성

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      // Enable sending automatic session events
      Amplitude.instance().trackingSessionEvents = true
      // Initialize SDK
      Amplitude.instance().initializeApiKey("API_KEY")
      // Set userId
      Amplitude.instance().setUserId("userId")
      // Log an event
      Amplitude.instance().logEvent("app_start")
          
      return true
    }

    앰플리튜드 객체를 생성

     

     

    서버 선택

    // For versions starting from 8.5.0
    // No need to call setServerUrl for sending data to Amplitude's EU servers
    Amplitude.instance().setServerZone(AMPServerZone.EU)
    
    // For earlier versions
    Amplitude.instance().setServerUrl("<https://api.eu.amplitude.com>")

     

     

     

    이벤트 전송

    기본 이벤트 전송

    Amplitude.instance().logEvent("Button Click")

    이벤트 이름만 남기고 싶을 때에는 이런 식으로 이벤트 이름만 적어주면 된다.

    예를 들어, 버튼을 눌렀을 때 그 버튼에 대한 이벤트 로그를 전송한다고 했을 때,

    @objc func mainButtonClicked(_ sender: UIButton) {
            Amplitude.instance().logEvent("mainButton_clicked")
        }
    

    이런 식으로 로그이벤트를 추가시켜주면 된다.

     

    이벤트 프로퍼티와 함께 이벤트 전송

    Amplitude.instance().logEvent("Button Clicked", withEventProperties: ["Hover Time": "100ms"] )

    위와 같음. 보내고 싶은 프로퍼티를 딕셔너리 형태로 보내주면 된다.

     

     

    유저 프로퍼티

    set

    let identify = AMPIdentify()
        .set("gender", value: "female")
        .set("age",value: NSNumber(value: 20))
    Amplitude.instance().identify(identify)

     

    setOnce

    set과는 다르게 딱 한번만 설정할 수 있다.

    이미 setOnce로 지정된 유저 프로퍼티가 있으면 무시된다.

    let identify1 = AMPIdentify().setOnce("sign_up_date", value: "2015-08-24")
    Amplitude.instance().identify(identify1)
    
    let identify2 = AMPIdentify().setOnce("sign_up_date", value: "2015-09-14")
    Amplitude.instance().identify(identify2) // Is ignored

     

    add

    numerical한 value를 가진 유저 프로퍼티의 값을 증가시킬 수 있다.

    만약 유저 프로퍼티가 set 되어 있지 않은 상태라면, 0을 기준으로 증가시킨다.

    value에 음수를 넣어서 더해줄 수도 있다.

    let identify = AMPIdentify()
        .add("karma", value: NSNumber(value: 0.123))
        .add("friends",value: NSNumber(value: 1))
    Amplitude.instance().identify(identify)

     

    한번에 여러 유저 프로퍼티를 지정하기

    Identify.set을 래핑한 기능.

    var userProperties: [AnyHashable : Any] = [:]
    userProperties["KEY"] = "VALUE"
    userProperties["OTHER_KEY"] = "OTHER_VALUE"
    Amplitude.instance().userProperties = userProperties

     

     

    유저 프로퍼티의 배열

    var colors: [AnyHashable] = []
    colors.append("rose")
    colors.append("gold")
    var numbers: [AnyHashable] = []
    numbers.append(NSNumber(value: 4))
    numbers.append(NSNumber(value: 5))
    let identify = AMPIdentify().set("colors", value: colors)
        .append("ab-tests", value: "campaign_a")
        .append("existing_list",value: numbers)
    Amplitude.instance().identify(identify)

    배열에 원하는 프로퍼티를 담아놓고, set을 이용해 설정할 수 있다.

    또는, append를 직접 해줄 수도 있다.

     

     

    prepend/append

    중복여부를 확인하지 않는다.

    중복여부를 확인해야할 경우 문서에서 preInsert 및 postInsert를 고려해볼 것

    var array: [AnyHashable] = []
    array.append("some_string")
    array.append(NSNumber(value: 56))
    let identify = AMPIdentify()
        .append("ab-tests", value: "new-user-test")
        .append("some_list",value: array)
    Amplitude.instance().identify(identify)

     

     

    preInsert/postInsert

    유저 프로퍼티에 해당 값이 존재하면, 작동하지 않는다.

    preInsert

    배열 내에 아직 존재하지 않는다면, 배열 맨 앞에 추가

    postInsert

    배열 내에 아직 존재하지 않는다면, 배열 맨 뒤에 추가

    Insert 및 append 모두 유저 프로퍼티가 존재하지 않는다면 새 값을 넣기 전 빈 배열을 초기화한다.

    var array: [AnyHashable] = []
    array.append("some_string")
    array.append(NSNumber(value: 56))
    let identify = AMPIdentify()
        .preInsert("ab-tests", value: "new-user-test")
        .preInsert("some_list",value: array)
    Amplitude.instance().identify(identify)

     

     

    유저 프로퍼티 제거

    모든 유저 프로퍼티 제거

    결과를 되돌릴 수 없음! 주의!

    Amplitude.instance().clearUserProperties()
    

    remove

    지울 대상이 없으면 작동하지 않는다.

    var array: [AnyHashable] = []
    array.append("some_string")
    array.append(NSNumber(value: 56))
    let identify = AMPIdentify()
        .remove("ab-tests", value: "new-user-test")
        .remove("some_list", value: array)
    Amplitude.instance().identify(identify)
    

     

    댓글

Designed by Tistory.