Custom Attributes

Prev Next

Overview

Custom Attributes in iOS enable developers to store and utilize user-specific information such as preferences, demographics, or activity data. These attributes enhance user engagement by allowing targeted and personalized communication.

Use Case

  1. Purpose:

    • Store user-specific data for personalized engagement.

    • Create user segments for tailored campaigns.

  2. Benefits:

    • Improve user satisfaction with relevant content and offers.

    • Facilitate data-driven insights for marketing and analytics.

Implementation Details

  1. Set Custom Attributes: Assign user-specific values such as strings, numbers, or dates.

    For Objective-C:

    // Set a string value
    [[Appoxee shared] setStringValue:@"JohnDoe" forKey:@"userName" withCompletionHandler:^(NSError * _Nullable error, id data) {
        if (!error) {
            NSLog(@"String attribute set successfully");
        }
    }];
    
    // Set a numeric value
    [[Appoxee shared] setNumberValue:@150 forKey:@"userScore" withCompletionHandler:^(NSError * _Nullable error, id data) {
        if (!error) {
            NSLog(@"Numeric attribute set successfully");
        }
    }];
    
    // Set a date value
    [[Appoxee shared] setDateValue:[NSDate date] forKey:@"userBirthday" withCompletionHandler:^(NSError * _Nullable error, id data) {
        if (!error) {
            NSLog(@"Date attribute set successfully");
        }
    }];
    
    // Set multiple custom attributes at once
    [[Appoxee shared] setCustomAttributes:@{
        @"userName": @"JohnDoe",
        @"userScore": @150,
        @"userBirthday": [NSDate date]
    } withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {
            NSLog(@"Custom attributes updated successfully (bulk).");
        }
    }];
    
    // Set alias with automatic attribute resync
    [[Appoxee shared] setDeviceAlias:@"user12345"
                  resendAttributes:YES
            withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {
            NSLog(@"Alias set successfully; attributes automatically re-synced.");
        }
    }];

    For Swift:

    // Set a string value
    Appoxee.shared()?.setStringValue("JohnDoe", forKey: "userName", withCompletionHandler: { (error, data) in
        if error == nil {
            print("String attribute set successfully")
        }
    })
    
    // Set a numeric value
    Appoxee.shared()?.setNumberValue(150, forKey: "userScore", withCompletionHandler: { (error, data) in
        if error == nil {
            print("Numeric attribute set successfully")
        }
    })
    
    // Set a date value
    Appoxee.shared()?.setDateValue(Date(), forKey: "userBirthday", withCompletionHandler: { (error, data) in
        if error == nil {
            print("Date attribute set successfully")
        }
    })
    
    // Set multiple custom attributes at once
    Appoxee.shared()?.setCustomAttributes([
        "userName": "JohnDoe",
        "userScore": 150,
        "userBirthday": Date()
    ]) { error in
        if error == nil {
            print("Custom attributes updated successfully (bulk).")
        }
    }
    
    // Set alias with automatic attribute resync
    Appoxee.shared()?.setDeviceAlias("user12345", resendAttributes: true) { error in
        if error == nil {
            print("Alias set successfully; attributes automatically re-synced.")
        }
    }

    Parameter behaviour

    • alias — The user’s identifier value.

    • attributes — Dictionary of attribute key–value pairs to set in one call.

    • resendAttributes — When true, triggers automatic re-sync of all previously set custom attributes after updating the alias.

    • completionHandler — Invoked with an NSError if the operation fails.

  2. Retrieve Custom Attributes: Fetch stored attributes for application logic or reporting.

    For Objective-C:

    [[Appoxee shared] fetchCustomFieldByKey:@"userName" withCompletionHandler:^(NSError *error, id data) {
        if (!error) {
            NSLog(@"Fetched Attribute: %@", data);
        }
    }];

    For Swift:

    Appoxee.shared()?.fetchCustomFieldByKey("userName", withCompletionHandler: { (error, data) in
        if error == nil {
            print("Fetched Attribute: \(data ?? "None")")
        }
    })

  3. Increment Numeric Attributes: Update numeric values dynamically.

    Appoxee.shared()?.incrementNumericKey("userScore", byNumericValue: 10, withCompletionHandler: { (error, data) in
        if error == nil {
            print("Numeric attribute incremented successfully")
        }
    })

Keep in mind:

  • Custom attributes can be leveraged for dynamic and personalized push messages.

  • Ensure attribute keys are consistent across platforms to maintain uniform data handling.

  • Test attribute setting and retrieval for accuracy and performance.