Skip to content

Commit

Permalink
urban airship integration support (#164)
Browse files Browse the repository at this point in the history
* porting connect integrations + urban airship code from obj c

* tagging $urban_airship_channel_id with ios or android

* decide response integrations using internal names instead of db ids

* urban_airship -> urbanairship

* fix a crash if the channelID is nil (not sure how it didn't show up in testing earlier)
  • Loading branch information
pchien authored Dec 11, 2017
1 parent 8e4b37b commit db9428d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Mixpanel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
E1F15FE21E64B60D00391AE3 /* Flush.swift in Sources */ = {isa = PBXBuildFile; fileRef = E115949E1D01BE14007F8B4F /* Flush.swift */; };
E1F15FE31E64B60D00391AE3 /* Track.swift in Sources */ = {isa = PBXBuildFile; fileRef = E11594A01D01C597007F8B4F /* Track.swift */; };
E1F15FE41E64B60D00391AE3 /* People.swift in Sources */ = {isa = PBXBuildFile; fileRef = E15FF7C71D0435670076CDE3 /* People.swift */; };
F05767541F8D909600499D9B /* ConnectIntegrations.swift in Sources */ = {isa = PBXBuildFile; fileRef = F05767531F8D909600499D9B /* ConnectIntegrations.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -232,6 +233,7 @@
E1D335CD1D30578E00E68E12 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
E1D335CF1D3059A800E68E12 /* AutomaticProperties.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AutomaticProperties.swift; sourceTree = "<group>"; };
E1F15FC91E64A10700391AE3 /* Mixpanel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Mixpanel.framework; sourceTree = BUILT_PRODUCTS_DIR; };
F05767531F8D909600499D9B /* ConnectIntegrations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectIntegrations.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -453,6 +455,7 @@
E189D8F91D5A6908007F3F29 /* Decide */ = {
isa = PBXGroup;
children = (
F05767521F8D905D00499D9B /* Connect Integrations */,
E189D8F51D54ECBF007F3F29 /* Decide.swift */,
E15422161D6FBA97009421A0 /* Swizzle.swift */,
E154221A1D70BEF0009421A0 /* Web Socket */,
Expand Down Expand Up @@ -544,6 +547,14 @@
name = "A/B Testing";
sourceTree = "<group>";
};
F05767521F8D905D00499D9B /* Connect Integrations */ = {
isa = PBXGroup;
children = (
F05767531F8D909600499D9B /* ConnectIntegrations.swift */,
);
name = "Connect Integrations";
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -781,6 +792,7 @@
E1D335CC1D303A0D00E68E12 /* FlushRequest.swift in Sources */,
E115234F1D7F58B000657E80 /* CGSizeToNSDictionary.swift in Sources */,
E154221F1D70DB09009421A0 /* WebSocket.swift in Sources */,
F05767541F8D909600499D9B /* ConnectIntegrations.swift in Sources */,
E15422151D6F9F44009421A0 /* Codeless.swift in Sources */,
E115948B1CFF1538007F8B4F /* Mixpanel.swift in Sources */,
E1497EF21D6B8DC900B2B65C /* CodelessBinding.swift in Sources */,
Expand Down
50 changes: 50 additions & 0 deletions Mixpanel/ConnectIntegrations.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// ConnectIntegrations.swift
// Mixpanel
//
// Created by Peter Chien on 10/10/17.
// Copyright © 2017 Mixpanel. All rights reserved.
//

class ConnectIntegrations {
open var mixpanel: MixpanelInstance?
var urbanAirshipRetries = 0
var savedUrbanAirshipChannelID: String?

open func setupIntegrations(_ integrations:[String]) {
if integrations.contains("urbanairship") {
self.setUrbanAirshipPeopleProp()
}
}

func setUrbanAirshipPeopleProp() {
if let urbanAirship = NSClassFromString("UAirship") {
let pushSelector = NSSelectorFromString("push")
if let pushIMP = urbanAirship.method(for: pushSelector) {
typealias pushFunc = @convention(c) (AnyObject, Selector) -> AnyObject!
let curriedImplementation = unsafeBitCast(pushIMP, to: pushFunc.self)
if let push = curriedImplementation(urbanAirship.self, pushSelector) {
if let channelID = push.perform(NSSelectorFromString("channelID"))?.takeUnretainedValue() as? String {
self.urbanAirshipRetries = 0
if (channelID != self.savedUrbanAirshipChannelID) {
self.mixpanel?.people.set(property: "$ios_urban_airship_channel_id", to: channelID)
self.savedUrbanAirshipChannelID = channelID
}
} else {
self.urbanAirshipRetries += 1
if self.urbanAirshipRetries <= ConnectIntegrationsConstants.uaMaxRetries {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
self.setUrbanAirshipPeopleProp()
}
}
}
}
}
}
}

open func reset() {
self.savedUrbanAirshipChannelID = nil
self.urbanAirshipRetries = 0
}
}
4 changes: 4 additions & 0 deletions Mixpanel/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ extension UIDevice {
}
}
#endif // !os(OSX)

struct ConnectIntegrationsConstants {
static let uaMaxRetries = 3
}
6 changes: 6 additions & 0 deletions Mixpanel/Decide.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ struct DecideResponse {
var newCodelessBindings: Set<CodelessBinding>
var newVariants: Set<Variant>
var toFinishVariants: Set<Variant>
var integrations: [String]

init() {
unshownInAppNotifications = []
newCodelessBindings = Set()
newVariants = Set()
toFinishVariants = Set()
integrations = []
}
}

Expand Down Expand Up @@ -129,6 +131,10 @@ class Decide {
self.automaticEventsEnabled = automaticEvents
}

if let integrations = result["integrations"] as? [String] {
decideResponse.integrations = integrations
}

self.decideFetched = true
semaphore.signal()
}
Expand Down
7 changes: 7 additions & 0 deletions Mixpanel/MixpanelInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele
#if DECIDE
let decideInstance: Decide
let automaticEvents = AutomaticEvents()
let connectIntegrations = ConnectIntegrations()
#endif // DECIDE

#if !os(OSX)
Expand Down Expand Up @@ -275,6 +276,7 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele
trackPushNotification(notification, event: "$app_open")
}
}
connectIntegrations.mixpanel = self
#endif // DECIDE
}
#else
Expand Down Expand Up @@ -412,6 +414,10 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele
self.markVariantRun(variant)
}
}

if decideResponse.integrations.count > 0 {
self.connectIntegrations.setupIntegrations(decideResponse.integrations)
}
}
}
}
Expand Down Expand Up @@ -733,6 +739,7 @@ extension MixpanelInstance {
self.decideInstance.decideFetched = false
self.decideInstance.ABTestingInstance.variants = Set()
self.decideInstance.codelessInstance.codelessBindings = Set()
self.connectIntegrations.reset()
MixpanelTweaks.defaultStore.reset()
#endif // DECIDE
}
Expand Down

0 comments on commit db9428d

Please sign in to comment.