グローバル・プロパティの設定
アプリから送信されるすべてのセッションとイベントに自動的にアタッチされるカスタムプロパティを定義することで、レポートでの詳細なデータセグメンテーションが可能になります。
グローバルプロパティを使用すると、必要なユーザー、アプリモード、またはコンテキスト情報を追跡できます。例えば、ゲームアプリでは、"Level "プロパティを作成し、"0 "に初期化します。すべてのセッションとイベントにこのプロパティが含まれ、セッション、イベントカウント、収益をユーザーレベル別に分析できます。
プロパティの仕様
制約と永続性
グローバル・プロパティの制約と永続化の動作を理解します。
- プロパティの最大数:アプリのインストールごとに最大5つのグローバルプロパティを定義できます。
- 永続性:プロパティは、明示的に設定が解除されるか、アプリがアンインストールされるまで、アプリの起動時に最新の値で永続化されます。
- 文字数制限:プロパティ名と値の長さは200文字までです。長い値は自動的に200文字に切り捨てられます。
- データの可用性:グローバルプロパティは、ユーザーレベルのエクスポートとポストバックでアクセスできます。集計レポートのサポートに関する最新情報については、Singularカスタマーサクセスマネージャーにお問い合わせください。
初期化時のグローバルプロパティの設定
SingularConfigで設定
SDK 初期化時にSingular.start() を呼び出す前にsetGlobalProperty メソッドを使用してグローバルプロパティを設定します。
グローバルプロパティはアプリの起動間で持続するため、プロパティはすでに異なる値で存在している可能性があります。overrideExistingパラメータを使用して、新しい値が既存の値を上書きするかどうかを制御します。
func getConfig() -> SingularConfig? {
// Create config with API credentials
guard let config = SingularConfig(apiKey: "SDK_KEY", andSecret: "SDK_SECRET") else {
return nil
}
// Set global properties during initialization
config.setGlobalProperty("MyProperty", withValue: "MyValue", overrideExisting: true)
config.setGlobalProperty("AnotherProperty", withValue: "AnotherValue", overrideExisting: true)
return config
}
// Initialize SDK with config
if let config = getConfig() {
Singular.start(config)
}
- (SingularConfig *)getConfig {
// Create config with API credentials
SingularConfig *config = [[SingularConfig alloc] initWithApiKey:@"SDK_KEY"
andSecret:@"SDK_SECRET"];
// Set global properties during initialization
[config setGlobalProperty:@"MyProperty" withValue:@"MyValue" overrideExisting:YES];
[config setGlobalProperty:@"AnotherProperty" withValue:@"AnotherValue" overrideExisting:YES];
return config;
}
// Initialize SDK with config
SingularConfig *config = [self getConfig];
[Singular start:config];
メソッドのシグネチャ:
- (void)setGlobalProperty:(NSString *)key withValue:(NSString *)value overrideExisting:(BOOL)overrideExisting;
パラメータ:
- キー:プロパティ名(最大200文字)
- 値:プロパティ値(200文字以内)
- overrideExisting:既存のプロパティを同じキーで上書きするかどうか。
初期化後のプロパティの管理
グローバル・プロパティの設定
アプリの実行中に任意の時点でグローバル・プロパティを追加または更新します。
// Set a global property after initialization
let result = Singular.setGlobalProperty("MyProperty",
andValue: "MyValue",
overrideExisting: true)
if result {
print("Property set successfully")
} else {
print("Failed to set property")
}
// Set a global property after initialization
BOOL result = [Singular setGlobalProperty:@"MyProperty"
andValue:@"MyValue"
overrideExisting:YES];
if (result) {
NSLog(@"Property set successfully");
} else {
NSLog(@"Failed to set property");
}
メソッドのシグネチャ:
+ (BOOL)setGlobalProperty:(NSString *)key andValue:(NSString *)value overrideExisting:(BOOL)overrideExisting;
戻り値:プロパティが正常に設定された場合はtrue 、そうでない場合はfalse
重要
-
重要:すでに 5 つのプロパティが存在し、新しいプロパティを追加しようとすると、このメソッドは
falseを返します。 -
overrideExistingパラメータは、既存のプロパティ値を置換するかどうかを決定します。 - 戻り値をチェックして、プロパティが正常に設定されたことを確認します。
グローバル・プロパティの取得
現在設定されているすべてのグローバル・プロパティとその値を Dictionary として取得します。
// Retrieve all global properties
let properties = Singular.getGlobalProperties()
// Iterate through properties
if let properties = properties as? [String: String] {
for (key, value) in properties {
print("Property: \(key) = \(value)")
}
}
// Retrieve all global properties
NSDictionary *properties = [Singular getGlobalProperties];
// Iterate through properties
for (NSString *key in properties) {
NSString *value = properties[key];
NSLog(@"Property: %@ = %@", key, value);
}
メソッドのシグニチャ:
+ (NSDictionary *)getGlobalProperties;
戻り値:すべてのグローバル・プロパティのキーと値のペアを含む Dictionary。
グローバル・プロパティのアンセット
特定のグローバル・プロパティをそのキーによって削除します。
// Remove a specific global property
Singular.unsetGlobalProperty("MyProperty")
// Remove a specific global property
[Singular unsetGlobalProperty:@"MyProperty"];
メソッドのシグニチャ:
+ (void)unsetGlobalProperty:(NSString *)key;
パラメータ:
- キー:削除するプロパティの名前。
すべてのグローバル・プロパティの削除
すべてのグローバル・プロパティを一度に削除する。
// Remove all global properties
Singular.clearGlobalProperties()
// Remove all global properties
[Singular clearGlobalProperties];
メソッドのシグネチャ:
+ (void)clearGlobalProperties;
ベストプラクティス:ユーザがログアウトしたときや、すべてのカスタムトラッキングプロパティをデフォルト状態にリセットする必要があるときに、clearGlobalProperties()。
実装例
完全な使用パターン
アプリケーションのライフサイクルを通じて、アプリレベルとユーザー固有のプロパティを追跡する。
// Initialize SDK with app-level global properties
func getConfig() -> SingularConfig? {
guard let config = SingularConfig(apiKey: "SDK_KEY", andSecret: "SDK_SECRET") else {
return nil
}
// Set app version as a global property
if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
config.setGlobalProperty("app_version", withValue: appVersion, overrideExisting: true)
}
return config
}
// Set third-party identifier on login
func onUserLogin(thirdPartyUserId: String) {
let success = Singular.setGlobalProperty("third_party_identifier",
andValue: thirdPartyUserId,
overrideExisting: true)
if success {
print("Third-party identifier set")
}
}
// Clear third-party identifier on logout
func onUserLogout() {
Singular.unsetGlobalProperty("third_party_identifier")
print("Third-party identifier cleared")
}
// Initialize SDK with app-level global properties
- (SingularConfig *)getConfig {
SingularConfig *config = [[SingularConfig alloc] initWithApiKey:@"SDK_KEY"
andSecret:@"SDK_SECRET"];
// Set app version as a global property
NSString *appVersion = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
if (appVersion) {
[config setGlobalProperty:@"app_version" withValue:appVersion overrideExisting:YES];
}
return config;
}
// Set third-party identifier on login
- (void)onUserLogin:(NSString *)thirdPartyUserId {
BOOL success = [Singular setGlobalProperty:@"third_party_identifier"
andValue:thirdPartyUserId
overrideExisting:YES];
if (success) {
NSLog(@"Third-party identifier set");
}
}
// Clear third-party identifier on logout
- (void)onUserLogout {
[Singular unsetGlobalProperty:@"third_party_identifier"];
NSLog(@"Third-party identifier cleared");
}
ベストプラクティスサードパーティのアナリティクス識別子(例:Mixpanel distinct_id、Amplitude user_id)をSingularのグローバルプロパティに同期して、統一されたクロスプラットフォームトラッキングを行う。ログイン時にユーザー固有の識別子を設定し、ログアウト時にunsetGlobalProperty() でクリアします。app_version のようなアプリレベルのプロパティは、セッションを超えて持続します。