LoginSignup
0
1

More than 1 year has passed since last update.

[iOS] Widget経由でアプリが起動された回数ってどのように計測するん?

Last updated at Posted at 2023-02-19

本記事について

「ロック/ホーム画面Widgetを経由でアプリが起動された回数」
をGoogle Analytics(GA)で計測する方法について紹介します。

環境

  • SwiftUI
  • Xcode: 14.2
  • Deployment Taget: 14.5
  • シミュレータ: 16.2

手順

この記事に全てが記載されています。
https://stackoverflow.com/questions/63697132/detect-app-launch-from-widgetkit-widget-extension

step1

WidgetのViewに.widgetURL("deeplinkのURL")のモディファイアを追加

step2

(SceneDelegateのタイミングでプロジェクトを作成した人は)下記のsceneデリゲートメソッドを追加

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
   // URLContexts.first?.urlでdeeplinkのURLを取得できる
   // urlのプロパティでhostやqueryなど指定することができる
}

step3

未起動、タスクがキルされた場合の考慮

step2のみだと、アプリのタスクがキルされた時には対応できない。
下記のようにSceneの起動時にもdeeplink経由したurlを取得することで、未起動、タスクがキルされた場合を考慮する。


func scene(
    _ scene: UIScene, willConnectTo session: UISceneSession,
    options connectionOptions: UIScene.ConnectionOptions
) {
 // connectionOptions.urlContexts.first?.urlで取得が可能
}

注意点

step3の未起動、タスクがキルされた状態では、アプリは非アクティブ状態になります。その状態でGAイベントを送信すると、イベントを許可されないというSDK側の制限があります。

実際に下記のログが発行されます。

xcodeのconsole.log
Cannot log screen view event when the app is not active

sdkを提供している公式githubのissueに解決策が記載されている。(応急処理レベルかも)
https://github.com/firebase/firebase-ios-sdk/issues/7577

下記で遅延を発生させて、処理を実行することで、正常にGAイベントが送信されます。

DispatchQueue.main.asyncAfter(deadline: .now() + 遅延時間) {
  // GAイベント送信処理
}

より適切な方法があれば、コメントいただけると幸いです。

以上です。

参考記事

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1