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 useUniqueDistinctId #972

Merged
merged 4 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 21 additions & 2 deletions Sources/Mixpanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (atomic) BOOL useIPAddressForGeoLocation;

/*!
Controls whether or not to use a unique device identifier for the Mixpanel Distinct ID

Defaults to NO
*/
@property (atomic) BOOL useUniqueDistinctId;

/*!
This allows enabling or disabling collecting common mobile events
Expand Down Expand Up @@ -251,6 +257,16 @@ NS_ASSUME_NONNULL_BEGIN
*/
+ (Mixpanel *)sharedInstanceWithToken:(NSString *)apiToken trackCrashes:(BOOL)trackCrashes;

/*!
Initializes a singleton instance of the API, using the unique device identifier for distinct_id and then returns it.

With the useUniqueDistinctId parameter, Mixpanel will use a unique device id for distinct_id.

@param apiToken your project token
@param useUniqueDistinctId whether or not to use the unique device identifier as the distinct_id
*/
+ (Mixpanel *)sharedInstanceWithToken:(NSString *)apiToken useUniqueDistinctId:(BOOL)useUniqueDistinctId;

/*!
Initializes a singleton instance of the API, uses it to track crashes, set whether or not to opt out tracking for
GDPR compliance, and then returns it.
Expand All @@ -261,8 +277,9 @@ NS_ASSUME_NONNULL_BEGIN
@param trackCrashes whether or not to track crashes in Mixpanel. may want to disable if you're seeing
issues with your crash reporting for either signals or exceptions
@param optOutTrackingByDefault whether or not to be opted out from tracking by default
@param useUniqueDistinctId whether or not to use the unique device identifier as the distinct_id
*/
+ (Mixpanel *)sharedInstanceWithToken:(NSString *)apiToken trackCrashes:(BOOL)trackCrashes optOutTrackingByDefault:(BOOL)optOutTrackingByDefault;
+ (Mixpanel *)sharedInstanceWithToken:(NSString *)apiToken trackCrashes:(BOOL)trackCrashes optOutTrackingByDefault:(BOOL)optOutTrackingByDefault useUniqueDistinctId:(BOOL)useUniqueDistinctId;

/*!
Returns a previously instantiated singleton instance of the API.
Expand All @@ -286,10 +303,12 @@ NS_ASSUME_NONNULL_BEGIN
@param flushInterval interval to run background flushing
@param trackCrashes whether or not to track crashes in Mixpanel. may want to disable if you're seeing
issues with your crash reporting for either signals or exceptions
@param useUniqueDistinctId whether or not to use the unique device identifier as the distinct_id
*/
- (instancetype)initWithToken:(NSString *)apiToken
flushInterval:(NSUInteger)flushInterval
trackCrashes:(BOOL)trackCrashes;
trackCrashes:(BOOL)trackCrashes
useUniqueDistinctId:(BOOL)useUniqueDistinctId;

/*!
Initializes an instance of the API with the given project token. This also sets
Expand Down
50 changes: 36 additions & 14 deletions Sources/Mixpanel.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ @implementation Mixpanel

+ (Mixpanel *)sharedInstanceWithToken:(NSString *)apiToken trackCrashes:(BOOL)trackCrashes
{
return [Mixpanel sharedInstanceWithToken:apiToken trackCrashes:trackCrashes optOutTrackingByDefault:NO];
return [Mixpanel sharedInstanceWithToken:apiToken trackCrashes:trackCrashes optOutTrackingByDefault:NO useUniqueDistinctId:NO];
}

+ (Mixpanel *)sharedInstanceWithToken:(NSString *)apiToken trackCrashes:(BOOL)trackCrashes optOutTrackingByDefault:(BOOL)optOutTrackingByDefault
+ (Mixpanel *)sharedInstanceWithToken:(NSString *)apiToken trackCrashes:(BOOL)trackCrashes optOutTrackingByDefault:(BOOL)optOutTrackingByDefault useUniqueDistinctId:(BOOL)useUniqueDistinctId
{
if (instances[apiToken]) {
return instances[apiToken];
Expand All @@ -59,7 +59,7 @@ + (Mixpanel *)sharedInstanceWithToken:(NSString *)apiToken trackCrashes:(BOOL)tr
const NSUInteger flushInterval = 60;
#endif

return [[self alloc] initWithToken:apiToken flushInterval:flushInterval trackCrashes:trackCrashes optOutTrackingByDefault:optOutTrackingByDefault];
return [[self alloc] initWithToken:apiToken flushInterval:flushInterval trackCrashes:trackCrashes optOutTrackingByDefault:optOutTrackingByDefault useUniqueDistinctId:useUniqueDistinctId];
}

+ (Mixpanel *)sharedInstanceWithToken:(NSString *)apiToken
Expand All @@ -70,7 +70,12 @@ + (Mixpanel *)sharedInstanceWithToken:(NSString *)apiToken

+ (Mixpanel *)sharedInstanceWithToken:(NSString *)apiToken optOutTrackingByDefault:(BOOL)optOutTrackingByDefault
{
return [Mixpanel sharedInstanceWithToken:apiToken trackCrashes:YES optOutTrackingByDefault:optOutTrackingByDefault];
return [Mixpanel sharedInstanceWithToken:apiToken trackCrashes:YES optOutTrackingByDefault:optOutTrackingByDefault useUniqueDistinctId:NO];
}

+ (Mixpanel *)sharedInstanceWithToken:(NSString *)apiToken useUniqueDistinctId:(BOOL)useUniqueDistinctId
{
return [Mixpanel sharedInstanceWithToken:apiToken trackCrashes:YES optOutTrackingByDefault:NO useUniqueDistinctId:useUniqueDistinctId];
}

+ (nullable Mixpanel *)sharedInstance
Expand Down Expand Up @@ -109,6 +114,7 @@ - (instancetype)initWithToken:(NSString *)apiToken
flushInterval:(NSUInteger)flushInterval
trackCrashes:(BOOL)trackCrashes
optOutTrackingByDefault:(BOOL)optOutTrackingByDefault
useUniqueDistinctId:(BOOL)useUniqueDistinctId
{
if (apiToken.length == 0) {
if (apiToken == nil) {
Expand All @@ -134,6 +140,7 @@ - (instancetype)initWithToken:(NSString *)apiToken
self.flushOnBackground = YES;

self.serverURL = @"https://api.mixpanel.com";
self.useUniqueDistinctId = useUniqueDistinctId;
self.distinctId = [self defaultDistinctId];
self.superProperties = [NSDictionary dictionary];
self.automaticProperties = [self collectAutomaticProperties];
Expand Down Expand Up @@ -175,19 +182,22 @@ - (instancetype)initWithToken:(NSString *)apiToken
- (instancetype)initWithToken:(NSString *)apiToken
flushInterval:(NSUInteger)flushInterval
trackCrashes:(BOOL)trackCrashes
useUniqueDistinctId:(BOOL)useUniqueDistinctId
{
return [self initWithToken:apiToken
flushInterval:flushInterval
trackCrashes:trackCrashes
optOutTrackingByDefault:NO];
optOutTrackingByDefault:NO
useUniqueDistinctId:useUniqueDistinctId];
}

- (instancetype)initWithToken:(NSString *)apiToken
andFlushInterval:(NSUInteger)flushInterval
{
return [self initWithToken:apiToken
flushInterval:flushInterval
trackCrashes:NO];
trackCrashes:NO
useUniqueDistinctId:NO];
}

- (void)dealloc
Expand Down Expand Up @@ -335,15 +345,13 @@ - (NSString *)defaultDistinctId
{
NSString *distinctId;
#if defined(MIXPANEL_UNIQUE_DISTINCT_ID)
#if !defined(MIXPANEL_WATCHOS) && !defined(MIXPANEL_MACOS)
if (!distinctId && NSClassFromString(@"UIDevice")) {
distinctId = [[UIDevice currentDevice].identifierForVendor UUIDString];
}
#elif defined(MIXPANEL_MACOS)
distinctId = [self macOSIdentifier];
#endif
distinctId = [self uniqueIdentifierForDevice];
#else
distinctId = [[NSUUID UUID] UUIDString];
if (self.useUniqueDistinctId) {
distinctId = [self uniqueIdentifierForDevice];
} else {
distinctId = [[NSUUID UUID] UUIDString];
}
#endif
if (!distinctId) {
MPLogInfo(@"%@ error getting device identifier: falling back to uuid", self);
Expand Down Expand Up @@ -998,6 +1006,20 @@ - (NSString *)macOSIdentifier
}
#endif


- (NSString *)uniqueIdentifierForDevice
{
NSString *distinctId;
jaredmixpanel marked this conversation as resolved.
Show resolved Hide resolved
#if !defined(MIXPANEL_WATCHOS) && !defined(MIXPANEL_MACOS)
if (!distinctId && NSClassFromString(@"UIDevice")) {
distinctId = [[UIDevice currentDevice].identifierForVendor UUIDString];
}
#elif defined(MIXPANEL_MACOS)
distinctId = [self macOSIdentifier];
#endif
return distinctId;
}

- (void)setCurrentRadio
{
dispatch_async(self.serialQueue, ^{
Expand Down