iOS SDK - Setting Global Properties

Setting Global Properties

Define custom properties that automatically attach to every session and event sent from your app, enabling detailed data segmentation in reports.

Global properties let you track any user, app mode, or contextual information you need. For example, in a gaming app, create a "Level" property initialized to "0" that updates as users progress. All sessions and events include this property, allowing you to analyze sessions, event counts, and revenue broken down by user level.

Property Specifications

Limits and Persistence

Understand the constraints and persistence behavior of global properties.

  • Maximum Properties: Define up to 5 global properties per app installation
  • Persistence: Properties persist between app launches with their most recent values until explicitly unset or the app is uninstalled
  • Character Limit: Property names and values can be up to 200 characters long. Longer values are automatically truncated to 200 characters
  • Data Availability: Global properties are accessible in user-level exports and postbacks. Contact your Singular customer success manager for updates on aggregate reporting support

Setting Global Properties at Initialization

Configure via SingularConfig

Set global properties during SDK initialization using the setGlobalProperty method before calling Singular.start() .

Since global properties persist between app launches, properties may already exist with different values. Use the overrideExisting parameter to control whether the new value should override existing values.

Swift Objective-C
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)
}

Method Signature:

- (void)setGlobalProperty:(NSString *)key withValue:(NSString *)value overrideExisting:(BOOL)overrideExisting;

Parameters:

  • key: Property name (max 200 characters)
  • value: Property value (max 200 characters)
  • overrideExisting: Whether to override an existing property with the same key

Managing Properties After Initialization

Set Global Property

Add or update a global property at any point during the app's runtime.

Swift Objective-C
// 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")
}

Method Signature:

+ (BOOL)setGlobalProperty:(NSString *)key andValue:(NSString *)value overrideExisting:(BOOL)overrideExisting;

Returns: true if the property was set successfully, false otherwise

Important — runtime +setGlobalProperty:andValue:overrideExisting: returns NO when:

  • The key is nil or empty
  • The SDK has not been started yet (+start: has not run)
  • 5 properties already exist and you attempt to add a new one
  • A property with the same key already exists and overrideExisting is NO — the value is not replaced

Always check the return value before assuming the property was stored.

Config-time vs runtime behavior: The config-time instance method -setGlobalProperty:withValue:overrideExisting: on SingularConfig returns void — it silently no-ops if the 5-property limit is reached or the key is empty. The runtime class method on Singular returns BOOL and exposes the failure. Prefer setting properties at config time when you can; use the runtime method when you need failure feedback.


Get Global Properties

Retrieve all currently set global properties and their values as a Dictionary.

Swift Objective-C
// 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)")
    }
}

Method Signature:

+ (NSDictionary *)getGlobalProperties;

Returns: A Dictionary containing all global property key-value pairs


Unset Global Property

Remove a specific global property by its key.

Swift Objective-C
// Remove a specific global property
Singular.unsetGlobalProperty("MyProperty")

Method Signature:

+ (void)unsetGlobalProperty:(NSString *)key;

Parameters:

  • key: The name of the property to remove

Clear All Global Properties

Remove all global properties at once.

Swift Objective-C
// Remove all global properties
Singular.clearGlobalProperties()

Method Signature:

+ (void)clearGlobalProperties;

Best Practice: Use clearGlobalProperties() when a user logs out or when you need to reset all custom tracking properties to their default state.


Implementation Example

Complete Usage Pattern

Track app-level and user-specific properties throughout the application lifecycle.

Swift Objective-C
// 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")
}

Best Practice: Sync third-party analytics identifiers (e.g., Mixpanel distinct_id, Amplitude user_id) to Singular global properties for unified cross-platform tracking. Set user-specific identifiers on login and clear them with unsetGlobalProperty() on logout. App-level properties like app_version persist across sessions.