Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an option for 'createAlias' for not calling identify #547

Merged
merged 2 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,16 @@
debugServiceExtension = "internal"
allowLocationSimulation = "YES"
launchAutomaticallySubstyle = "8">
<RemoteRunnable
runnableDebuggingMode = "2"
BundleIdentifier = "com.apple.Carousel"
RemotePath = "/MixpanelDemo">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "86F86E8A22440C5C00B69832"
BuildableName = "MixpanelDemoWatch.app"
BlueprintName = "MixpanelDemoWatch"
ReferencedContainer = "container:MixpanelDemo.xcodeproj">
</BuildableReference>
</RemoteRunnable>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand All @@ -75,27 +73,16 @@
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES"
launchAutomaticallySubstyle = "8">
<RemoteRunnable
runnableDebuggingMode = "2"
BundleIdentifier = "com.apple.Carousel"
RemotePath = "/MixpanelDemo">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "86F86E8A22440C5C00B69832"
BuildableName = "MixpanelDemoWatch.app"
BlueprintName = "MixpanelDemoWatch"
ReferencedContainer = "container:MixpanelDemo.xcodeproj">
</BuildableReference>
</RemoteRunnable>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "86F86E8A22440C5C00B69832"
BuildableName = "MixpanelDemoWatch.app"
BlueprintName = "MixpanelDemoWatch"
ReferencedContainer = "container:MixpanelDemo.xcodeproj">
</BuildableReference>
</MacroExpansion>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,46 +54,33 @@
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<RemoteRunnable
runnableDebuggingMode = "2"
BundleIdentifier = "com.apple.Carousel"
RemotePath = "/MixpanelDemo">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "86F86E8A22440C5C00B69832"
BuildableName = "MixpanelDemoWatch.app"
BlueprintName = "MixpanelDemoWatch"
ReferencedContainer = "container:MixpanelDemo.xcodeproj">
</BuildableReference>
</RemoteRunnable>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<RemoteRunnable
runnableDebuggingMode = "2"
BundleIdentifier = "com.apple.Carousel"
RemotePath = "/MixpanelDemo">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "86F86E8A22440C5C00B69832"
BuildableName = "MixpanelDemoWatch.app"
BlueprintName = "MixpanelDemoWatch"
ReferencedContainer = "container:MixpanelDemo.xcodeproj">
</BuildableReference>
</RemoteRunnable>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "86F86E8A22440C5C00B69832"
BuildableName = "MixpanelDemoWatch.app"
BlueprintName = "MixpanelDemoWatch"
ReferencedContainer = "container:MixpanelDemo.xcodeproj">
</BuildableReference>
</MacroExpansion>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
Expand Down
36 changes: 36 additions & 0 deletions MixpanelDemo/MixpanelDemoTests/MixpanelDemoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,42 @@ class MixpanelDemoTests: MixpanelBaseTests {
}
removeDBfile(testMixpanel.apiToken)
}

func testCreateAlias() {
let testMixpanel = Mixpanel.initialize(token: randomId(), flushInterval: 60)
testMixpanel.people.set(properties: ["p1": "a"])
waitForTrackingQueue(testMixpanel)
var unidentifiedQueue = unIdentifiedPeopleQueue(token: testMixpanel.apiToken)
// the user profile update has been queued up in unidentifiedQueue until identify is called
XCTAssertTrue(!unidentifiedQueue.isEmpty)

let distinctId = testMixpanel.distinctId
let alias: String = "a1"
testMixpanel.createAlias(alias, distinctId: testMixpanel.distinctId)
waitForTrackingQueue(testMixpanel)

let mixpanelIdentity = MixpanelPersistence.loadIdentity(apiToken: testMixpanel.apiToken)
XCTAssertTrue(distinctId == mixpanelIdentity.distinctID && distinctId == mixpanelIdentity.peopleDistinctID && distinctId == mixpanelIdentity.userId && alias == mixpanelIdentity.alias)
removeDBfile(testMixpanel.apiToken)
unidentifiedQueue = unIdentifiedPeopleQueue(token: testMixpanel.apiToken)
// unidentifiedQueue has been flushed
XCTAssertTrue(unidentifiedQueue.isEmpty)

let testMixpanel2 = Mixpanel.initialize(token: randomId(), flushInterval: 60)
testMixpanel2.people.set(properties: ["p1": "a"])
waitForTrackingQueue(testMixpanel2)

let distinctId2 = testMixpanel2.distinctId
testMixpanel2.createAlias(alias, distinctId: testMixpanel.distinctId, andIdentify: false)
waitForTrackingQueue(testMixpanel2)

let unidentifiedQueue2 = unIdentifiedPeopleQueue(token: testMixpanel2.apiToken)
// The user profile updates should still be held in unidentifiedQueue cause no identify is called
XCTAssertTrue(!unidentifiedQueue2.isEmpty)
let mixpanelIdentity2 = MixpanelPersistence.loadIdentity(apiToken: testMixpanel2.apiToken)
XCTAssertTrue(distinctId2 == mixpanelIdentity2.distinctID && nil == mixpanelIdentity2.peopleDistinctID && nil == mixpanelIdentity2.userId && alias == mixpanelIdentity2.alias)
removeDBfile(testMixpanel2.apiToken)
}

func testPersistentIdentity() {
let testMixpanel = Mixpanel.initialize(token: randomId(), flushInterval: 60)
Expand Down
17 changes: 15 additions & 2 deletions Sources/MixpanelInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,12 @@ extension MixpanelInstance {
self.hadPersistedDistinctId = true
}

if self.userId == nil {
self.readWriteLock.write {
self.userId = distinctId
}
}

if distinctId != self.distinctId {
let oldDistinctId = self.distinctId
self.readWriteLock.write {
Expand Down Expand Up @@ -652,6 +658,9 @@ extension MixpanelInstance {
The alias method creates an alias which Mixpanel will use to remap one id to another.
Multiple aliases can point to the same identifier.

Please note: With Mixpanel Identity Merge enabled, calling alias is no longer required
but can be used to merge two IDs in scenarios where identify() would fail


`mixpanelInstance.createAlias("New ID", distinctId: mixpanelInstance.distinctId)`

Expand All @@ -663,10 +672,12 @@ extension MixpanelInstance {
- parameter alias: A unique identifier that you want to use as an identifier for this user.
- parameter distinctId: The current user identifier.
- parameter usePeople: boolean that controls whether or not to set the people distinctId to the event distinctId.
- parameter andIdentify: an optional boolean that controls whether or not to call 'identify' with your current
user identifier(not alias). Default to true for keeping your signup funnels working correctly in most cases.
zihejia marked this conversation as resolved.
Show resolved Hide resolved
- parameter completion: an optional completion handler for when the createAlias has completed.
This should only be set to false if you wish to prevent people profile updates for that user.
*/
open func createAlias(_ alias: String, distinctId: String, usePeople: Bool = true, completion: (() -> Void)? = nil) {
open func createAlias(_ alias: String, distinctId: String, usePeople: Bool = true, andIdentify: Bool = true, completion: (() -> Void)? = nil) {
if hasOptedOutTracking() {
if let completion = completion {
DispatchQueue.main.async(execute: completion)
Expand Down Expand Up @@ -731,7 +742,9 @@ extension MixpanelInstance {

let properties = ["distinct_id": distinctId, "alias": alias]
track(event: "$create_alias", properties: properties)
identify(distinctId: distinctId, usePeople: usePeople)
if andIdentify {
identify(distinctId: distinctId, usePeople: usePeople)
}
flush(completion: completion)
} else {
Logger.error(message: "alias: \(alias) matches distinctId: \(distinctId) - skipping api call.")
Expand Down