広告を連携する方法

    広告を連携する方法


    記事の要約

    複数の広告チャネルを経由する場合は、ディープリンクまたはウェブリンクのアドレスにクエリパラメータを付与することで、継続的にトラッキングを行うことができます。
    以下は、ディープリンクからのリクエストを想定した例です。

    ShopLive SDK にクエリパラメータを渡す方法

    class SceneDelegate: UIResponder, UIWindowSceneDelegate { 
        func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
            guard let urlContext = URLContexts.first else { return }
            let url = urlContext.url
            handleUrl(url : url)
        }
        func handleUrl(url : URL) {
            // 1. URLComponentsでURLを分解
            guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
                  let queryItems = components.queryItems else {
                print("No query parameters found.")
                return
            }
            // 2. [String: String] の形式に変換
            let params: [String: String] = queryItems.reduce(into: [:]) { result, item in
                result[item.name] = item.value ?? ""
            }
            // 3. 広告連携に必要なクエリをShopLive SDKに設定
            for (key,value) in params {
                 if key == "utm_source" {
                     ShopLiveCommon.setUtmSource(utmSource: value)
                 }
                 else if key == "utm_medium" {
                     ShopLiveCommon.setUtmMedium(utmMedium: value)
                 }
                 else if key == "utm_campaign" {
                     ShopLiveCommon.setUtmCampaign(utmCampaign: value)
                 }
                 else if key == "utm_content" {
                     ShopLiveCommon.setUtmContent(utmContent: value)
                 }
                 else {
                     ShopLive.addParameter(key: key, value: value)
                 }
            }
            ShopLive.play()
        }
    }