MoEngageInAppFCStateManager

extension MoEngageInAppFCStateManager
  • Generates a canonical FC counter key for a rule and scope

    The key format is: fc:<scope>:<unit>:p=<period>:b=<bucket>

    Declaration

    Swift

    public func generateCounterKey(for rule: MoEngageInAppFCRule, scope: MoEngageFCScope) -> String

    Parameters

    rule

    The FC rule containing unit and period information

    scope

    The business scope (global, session, screen, event, tag, campaign)

    Return Value

    Canonical FC counter key

  • Note

    Device ID must not contain : character as it’s used as key separator

    Declaration

    Swift

    public func generatePerTagCounterKey(for rule: MoEngageInAppFCTagRule, scope: MoEngageFCScope, tag: String, deviceId: String? = nil) -> String

    Parameters

    rule

    The FC tag rule containing unit and period information

    scope

    The business scope (tagAll or tagAny)

    tag

    The tag string (will be lowercased)

    deviceId

    Optional device ID to include in key (for multi-device storage from template calls)

    Return Value

    Canonical FC counter key for the tag, optionally with device tag

  • Note: Campaign counter keys do NOT include device tags because:

    • Campaign counters are incremented locally after showing in-app (assumed current device)
    • They are not ingested from template payloads (which would need device tags)
    • This keeps local increments simple and device-scoped

    Declaration

    Swift

    public func generateCampaignCounterKey(for campaign: MoEngageInAppCampaignMeta, rule: MoEngageInAppFCRule) -> String

Device Frequency API

Frequency Data Conversion

  • Get frequency data for current device only (for sync)

    Filters counters to only include:

    • Keys without device tag (local increments, assumed current device)
    • Keys with current device tag (from template ingestion)

    This reduces sync payload size by excluding other devices’ data.

    Backend Payload Format

    {
      "LH": {                    // Last Hour bucket
        "a_c": 5,                // All campaigns counter
        "s_s": 2,                // Session start counter
        "s_l": 3,                // Screen load counter
        "c_e": 1,                // Custom event counter
        "all_t": 4,              // All tags counter
        "any_t": 2               // Any tag counter
      },
      "010125": {                // Day bucket (DDMMYY format, UTC)
        "a_c": 10,
        "s_l": 5,
        ...
      }
    }
    

    Declaration

    Swift

    public func getFrequencyDataForCurrentDevice(deviceId: String, sessionFCStore: MoEngageInAppSessionFCStore? = nil) -> [String : Any]

    Parameters

    deviceId

    Current device identifier for filtering

    sessionFCStore

    Optional session FC store to include session counters in sync

    Return Value

    Dictionary keyed by bucket (e.g., “LH”, “DDMMYY”) with counter keys and counts (current device only)

  • Get frequency data for a device in backend payload format

    Note: This method returns ALL counters (for backward compatibility). For sync operations, use getFrequencyDataForCurrentDevice() instead to filter to current device only.

    Backend Payload Format

    {
      "LH": {                    // Last Hour bucket
        "a_c": 5,                // All campaigns counter
        "s_s": 2,                // Session start counter
        "s_l": 3,                // Screen load counter
        "c_e": 1,                // Custom event counter
        "all_t": 4,              // All tags counter
        "any_t": 2               // Any tag counter
      },
      "010125": {                // Day bucket (DDMMYY format, UTC)
        "a_c": 10,
        "s_l": 5,
        ...
      }
    }
    

    Counter Key Mappings

    • a_c = All campaigns (global scope)
    • s_s = Session start (session scope)
    • s_l = Screen load (screen scope)
    • c_e = Custom event (event scope)
    • all_t = All tags (tag scope, AND operator)
    • any_t = Any tag (tag scope, OR operator)

    Bucket Formats

    • "LH" = Last Hour (current hour bucket)
    • "DDMMYY" = Day bucket (e.g., “010125” = Jan 1, 2025)

    Declaration

    Swift

    public func getFrequencyData(for deviceId: String) -> [String : Any]

    Parameters

    deviceId

    Device identifier (kept for API compatibility)

    Return Value

    Dictionary keyed by bucket (e.g., “LH”, “DDMMYY”) with counter keys and counts

  • Reset frequency data for a device

    Removes all counters associated with the specified device:

    • Keys with device tag matching the deviceId (exact match)
    • Keys without device tag (assumed current device if deviceId matches current device)

    Declaration

    Swift

    public func resetFrequency(for deviceId: String)

    Parameters

    deviceId

    Device identifier for which to reset counters

Counter Operations

  • Increments counter without returning the updated value.

    Stops incrementing at Int.max to prevent overflow.

    Declaration

    Swift

    public func incrementCounter(for key: String)
  • Returns counter value with optional cross-device aggregation.

    When aggregating, sums all counters with matching base keys (ignoring device tags).

    Declaration

    Swift

    public func getCounter(for key: String, aggregateAcrossDevices: Bool = true) -> Int

    Parameters

    key

    Counter key to read

    aggregateAcrossDevices

    Whether to sum matching counters across all devices

    Return Value

    Counter value (0 if not found), or aggregated sum if enabled

Maintenance

  • Returns all counters.

    Declaration

    Swift

    public func getAllCounters() -> [String : Int]
  • Clears all counters in memory and on disk.

    Declaration

    Swift

    public func clearAllCounters()