Last active
November 27, 2019 19:25
-
-
Save codelance/fac78845571ae6328e691b6b42e98e0e to your computer and use it in GitHub Desktop.
Segment -> AppsFlyer Integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func initialize() { | |
guard let projectId = UIApplication.segmentProjectId else { | |
return | |
} | |
//#if DEBUG | |
//SEGAnalytics.debug(true) | |
//#endif | |
let config = SEGAnalyticsConfiguration(writeKey: projectId) | |
config.use(SEGAppsFlyerIntegrationFactory.create(withLaunch: UIApplication.shared)) | |
config.recordScreenViews = true | |
config.trackApplicationLifecycleEvents = true | |
config.trackPushNotifications = true | |
config.trackDeepLinks = true | |
config.trackAttributionData = true | |
config.enableAdvertisingTracking = true | |
let mixpanelScreenTrack = SEGBlockMiddleware { (context, next) in | |
if context.eventType == .screen { | |
if let screen = context.payload as? SEGScreenPayload { | |
Mixpanel.mainInstance().track(event: "ScreenViewed", properties: [ | |
"screen_name": screen.name | |
]) | |
Mixpanel.mainInstance().registerSuperProperties(["screen_name": screen.name]) | |
} | |
next(context) | |
} | |
} | |
config.middlewares = [ | |
mixpanelScreenTrack | |
] | |
SEGAnalytics.setup(with: config) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIApplication : SEGAppsFlyerTrackerDelegate { | |
public func onConversionDataReceived(_ installData: [AnyHashable : Any]!) { | |
guard let first_launch_flag = installData["is_first_launch"] as? Int else { | |
return | |
} | |
guard let status = installData["af_status"] as? String else { | |
return | |
} | |
if(first_launch_flag == 1) { | |
if(status == "Non-organic") { | |
if let media_source = installData["media_source"] , let campaign = installData["campaign"]{ | |
logger.info("This is a Non-Organic install. Media source: \(media_source) Campaign: \(campaign)") | |
Mixpanel.mainInstance().track(event: "FirstAppLaunch", properties: [ | |
"media_source": String(describing: media_source), | |
"campaign": String(describing: campaign) | |
]) | |
} | |
} else { | |
logger.debug("This is an organic install.") | |
} | |
} else { | |
logger.debug("Not First Launch") | |
} | |
} | |
public func onConversionDataRequestFailure(_ error: Error!) { | |
if let err = error{ | |
logger.warning(err) | |
} | |
} | |
public func onAppOpenAttribution(_ attributionData: [AnyHashable : Any]!) { | |
if let data = attributionData{ | |
logger.warning("\(data)") | |
} | |
} | |
public func onAppOpenAttributionFailure(_ error: Error!) { | |
if let err = error{ | |
logger.warning(err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment