Files
purchases-ios/Sources/SubscriberAttributes/SubscriberAttributesManager+Appstack.swift
Damian RubioandRevenueCat Git Bot 496b560a66 [CIA-5346] Appstack integration to ios sdk (#6366)
* feat(appstack): adds attribution data capabilities from appstack

* fix(appstack): adds missing new manager to xcodeproject

* feat(appstack): updated appstack integration to perform offerings sync

* [skip ci] Update baseline swiftinterface files (#6372)

* fix(appstack): fixing appstack parameter type to match what their sdk is producing.

* fix(appstack): fixed edge case when delegate is nil

* fix(appstack): decouple sync+offerings from AttributionDelegate and guarantee completion fallback

* chore(appstack): cleaning dead code after delegate fixing

---------

Co-authored-by: RevenueCat Git Bot <72824662+RCGitBot@users.noreply.github.com>
2026-03-06 10:57:39 +01:00

97 lines
3.5 KiB
Swift

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
import Foundation
extension SubscriberAttributesManager {
func setAppstackAttributionParams(_ data: [String: Any]?, appUserID: String) {
guard let data = data else { return }
setAppstackCampaignAttributes(from: data, appUserID: appUserID)
setAppstackClickIDs(from: data, appUserID: appUserID)
setAppstackIDAttribute(from: data, appUserID: appUserID)
}
}
private extension SubscriberAttributesManager {
enum AppstackAttributionKeys {
static let appstackID = "appstack_id"
static let adNetwork = "appstack_adnetwork"
static let campaign = "appstack_campaign"
static let adSet = "appstack_adset"
static let adKey = "appstack_ad"
static let keywords = "appstack_keywords"
static let clickIDs = ["fbclid", "gclid", "wbraid", "gbraid", "ttclid"]
}
func setAppstackCampaignAttributes(from data: [String: Any], appUserID: String) {
if let mediaSource = stringValueForAppstackData(from: data, key: AppstackAttributionKeys.adNetwork) {
setMediaSource(mediaSource, appUserID: appUserID)
setAttributes([AppstackAttributionKeys.adNetwork: mediaSource], appUserID: appUserID)
}
if let campaign = stringValueForAppstackData(from: data, key: AppstackAttributionKeys.campaign) {
setCampaign(campaign, appUserID: appUserID)
setAttributes([AppstackAttributionKeys.campaign: campaign], appUserID: appUserID)
}
if let adGroup = stringValueForAppstackData(from: data, key: AppstackAttributionKeys.adSet) {
setAdGroup(adGroup, appUserID: appUserID)
setAttributes([AppstackAttributionKeys.adSet: adGroup], appUserID: appUserID)
}
// swiftlint:disable:next identifier_name
if let ad = stringValueForAppstackData(from: data, key: AppstackAttributionKeys.adKey) {
setAd(ad, appUserID: appUserID)
setAttributes([AppstackAttributionKeys.adKey: ad], appUserID: appUserID)
}
if let keyword = stringValueForAppstackData(from: data, key: AppstackAttributionKeys.keywords) {
setKeyword(keyword, appUserID: appUserID)
setAttributes([AppstackAttributionKeys.keywords: keyword], appUserID: appUserID)
}
}
func setAppstackClickIDs(from data: [String: Any], appUserID: String) {
for key in AppstackAttributionKeys.clickIDs {
if let value = stringValueForAppstackData(from: data, key: key) {
setAttributes([key: value], appUserID: appUserID)
}
}
}
func setAppstackIDAttribute(from data: [String: Any], appUserID: String) {
if let appstackID = stringValueForAppstackData(from: data, key: AppstackAttributionKeys.appstackID) {
setAppstackID(appstackID, appUserID: appUserID)
}
}
func stringValueForAppstackData(from data: [String: Any], key: String) -> String? {
guard let value = data[key] else { return nil }
if let stringValue = value as? String {
return stringValue.isEmpty ? nil : stringValue
}
if let boolValue = value as? Bool { return String(boolValue) }
if let number = value as? NSNumber {
return number.stringValue
}
return nil
}
}