Add Mixpanel analytics for iOS

This is mostly copied and then modified from the Realm Cocoa repo.
This commit is contained in:
Scott Kyle 2016-02-16 22:32:56 -08:00
parent 18b0d4bf49
commit 6f90a3a6e8
4 changed files with 213 additions and 0 deletions

View File

@ -86,6 +86,8 @@
F63FF3311C16434400B3B8E0 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = F63FF3301C16434400B3B8E0 /* libz.tbd */; };
F68A278C1BC2722A0063D40A /* RJSModuleLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = F68A278B1BC2722A0063D40A /* RJSModuleLoader.m */; };
F6BB7DF21BF681BC00D0A69E /* base64.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F6BB7DF01BF681BC00D0A69E /* base64.hpp */; };
F6C74DF01C732CC500C9DDCD /* RealmAnalytics.h in Headers */ = {isa = PBXBuildFile; fileRef = F6C74DEE1C732CC500C9DDCD /* RealmAnalytics.h */; };
F6C74DF11C732CC500C9DDCD /* RealmAnalytics.mm in Sources */ = {isa = PBXBuildFile; fileRef = F6C74DEF1C732CC500C9DDCD /* RealmAnalytics.mm */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -247,6 +249,8 @@
F6BB7DEF1BF681BC00D0A69E /* base64.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = base64.cpp; sourceTree = "<group>"; };
F6BB7DF01BF681BC00D0A69E /* base64.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = base64.hpp; sourceTree = "<group>"; };
F6C3FBBC1BF680EC00E6FFD4 /* json.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = json.hpp; sourceTree = "<group>"; };
F6C74DEE1C732CC500C9DDCD /* RealmAnalytics.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RealmAnalytics.h; path = "react-native/RealmAnalytics.h"; sourceTree = "<group>"; };
F6C74DEF1C732CC500C9DDCD /* RealmAnalytics.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = RealmAnalytics.mm; path = "react-native/RealmAnalytics.mm"; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -334,6 +338,8 @@
022D59301C043340001E25FE /* ReactTests.xcodeproj */,
0270BCCF1B7D067300010E03 /* RealmReact.h */,
0270BCD01B7D067300010E03 /* RealmReact.mm */,
F6C74DEE1C732CC500C9DDCD /* RealmAnalytics.h */,
F6C74DEF1C732CC500C9DDCD /* RealmAnalytics.mm */,
);
name = RealmReact;
sourceTree = "<group>";
@ -550,6 +556,7 @@
buildActionMask = 2147483647;
files = (
F636F6C81BCDB3570023F35C /* RealmReact.h in Headers */,
F6C74DF01C732CC500C9DDCD /* RealmAnalytics.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -770,6 +777,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F6C74DF11C732CC500C9DDCD /* RealmAnalytics.mm in Sources */,
0270BCD11B7D067300010E03 /* RealmReact.mm in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;

View File

@ -0,0 +1,15 @@
/* Copyright 2016 Realm Inc - All Rights Reserved
* Proprietary and Confidential
*/
#import <Foundation/Foundation.h>
#ifdef __cplusplus
extern "C" {
#endif
void RLMSendAnalytics();
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,181 @@
/* Copyright 2016 Realm Inc - All Rights Reserved
* Proprietary and Confidential
*/
#import "RealmAnalytics.h"
#if TARGET_IPHONE_SIMULATOR
#import <CommonCrypto/CommonDigest.h>
#import <sys/socket.h>
#import <sys/sysctl.h>
#import <net/if.h>
#import <net/if_dl.h>
#import <array>
// This symbol is defined by the Apple Generic versioning system when building this project.
// It confusingly looks like this: @(#)PROGRAM:RealmReact PROJECT:RealmJS-0.0.1
extern "C" const char RealmReactVersionString[];
// Wrapper for sysctl() that handles the memory management stuff
static auto RLMSysCtl(int *mib, u_int mibSize, size_t *bufferSize) {
std::unique_ptr<void, decltype(&free)> buffer(nullptr, &free);
int ret = sysctl(mib, mibSize, nullptr, bufferSize, nullptr, 0);
if (ret != 0) {
return buffer;
}
buffer.reset(malloc(*bufferSize));
if (!buffer) {
return buffer;
}
ret = sysctl(mib, mibSize, buffer.get(), bufferSize, nullptr, 0);
if (ret != 0) {
buffer.reset();
}
return buffer;
}
// Get the version of OS X we're running on (even in the simulator this gives
// the OS X version and not the simulated iOS version)
static NSString *RLMOSVersion() {
std::array<int, 2> mib = {CTL_KERN, KERN_OSRELEASE};
size_t bufferSize;
auto buffer = RLMSysCtl(&mib[0], mib.size(), &bufferSize);
if (!buffer) {
return nil;
}
return [[NSString alloc] initWithBytesNoCopy:buffer.release()
length:bufferSize - 1
encoding:NSUTF8StringEncoding
freeWhenDone:YES];
}
// Hash the data in the given buffer and convert it to a hex-format string
static NSString *RLMHashData(const void *bytes, size_t length) {
unsigned char buffer[CC_SHA256_DIGEST_LENGTH];
CC_SHA256(bytes, static_cast<CC_LONG>(length), buffer);
char formatted[CC_SHA256_DIGEST_LENGTH * 2 + 1];
for (int i = 0; i < CC_SHA256_DIGEST_LENGTH; ++i) {
sprintf(formatted + i * 2, "%02x", buffer[i]);
}
return [[NSString alloc] initWithBytes:formatted
length:CC_SHA256_DIGEST_LENGTH * 2
encoding:NSUTF8StringEncoding];
}
// Returns the hash of the MAC address of the first network adaptor since the
// vendorIdentifier isn't constant between iOS simulators.
static NSString *RLMMACAddress() {
int en0 = static_cast<int>(if_nametoindex("en0"));
if (!en0) {
return nil;
}
std::array<int, 6> mib = {CTL_NET, PF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, en0};
size_t bufferSize;
auto buffer = RLMSysCtl(&mib[0], mib.size(), &bufferSize);
if (!buffer) {
return nil;
}
// sockaddr_dl struct is immediately after the if_msghdr struct in the buffer
auto sockaddr = reinterpret_cast<sockaddr_dl *>(static_cast<if_msghdr *>(buffer.get()) + 1);
auto mac = reinterpret_cast<const unsigned char *>(sockaddr->sdl_data + sockaddr->sdl_nlen);
return RLMHashData(mac, 6);
}
static bool RLMIsDebuggerAttached() {
int name[] = {
CTL_KERN,
KERN_PROC,
KERN_PROC_PID,
getpid()
};
struct kinfo_proc info;
size_t info_size = sizeof(info);
if (sysctl(name, sizeof(name) / sizeof(name[0]), &info, &info_size, NULL, 0) == -1) {
NSLog(@"sysctl() failed: %s", strerror(errno));
return false;
}
return (info.kp_proc.p_flag & P_TRACED) != 0;
}
static NSDictionary *RLMAnalyticsPayload() {
static NSString * const kUnknownString = @"unknown";
NSBundle *appBundle = NSBundle.mainBundle;
NSString *hashedBundleID = appBundle.bundleIdentifier;
NSString *hashedMACAddress = RLMMACAddress();
// Main bundle isn't always the one of interest (e.g. when running tests
// it's xctest rather than the app's bundle), so look for one with a bundle ID
if (!hashedBundleID) {
for (NSBundle *bundle in NSBundle.allBundles) {
if ((hashedBundleID = bundle.bundleIdentifier)) {
appBundle = bundle;
break;
}
}
}
// If we found a bundle ID anywhere, hash it as it could contain sensitive
// information (e.g. the name of an unnanounced product)
if (hashedBundleID) {
NSData *data = [hashedBundleID dataUsingEncoding:NSUTF8StringEncoding];
hashedBundleID = RLMHashData(data.bytes, data.length);
}
return @{
@"event": @"Run",
@"properties": @{
// MixPanel properties
@"token": @"ce0fac19508f6c8f20066d345d360fd0",
// Anonymous identifiers to deduplicate events
@"distinct_id": hashedMACAddress ?: kUnknownString,
@"Anonymized MAC Address": hashedMACAddress ?: kUnknownString,
@"Anonymized Bundle ID": hashedBundleID ?: kUnknownString,
// Which version of Realm is being used
@"Binding": @"react-native",
@"Language": @"js",
@"Realm Version": [[@(RealmReactVersionString) componentsSeparatedByString:@"-"] lastObject] ?: kUnknownString,
@"Target OS Type": @"ios",
// Current OS version the app is targetting
@"Target OS Version": [[NSProcessInfo processInfo] operatingSystemVersionString],
// Minimum OS version the app is targetting
@"Target OS Minimum Version": appBundle.infoDictionary[@"MinimumOSVersion"] ?: kUnknownString,
// Host OS version being built on
@"Host OS Type": @"osx",
@"Host OS Version": RLMOSVersion() ?: kUnknownString,
}
};
}
void RLMSendAnalytics() {
if (getenv("REALM_DISABLE_ANALYTICS") || !RLMIsDebuggerAttached()) {
return;
}
NSData *payload = [NSJSONSerialization dataWithJSONObject:RLMAnalyticsPayload() options:0 error:nil];
NSString *url = [NSString stringWithFormat:@"https://api.mixpanel.com/track/?data=%@&ip=1", [payload base64EncodedStringWithOptions:0]];
// No error handling or anything because logging errors annoyed people for no
// real benefit, and it's not clear what else we could do
[[NSURLSession.sharedSession dataTaskWithURL:[NSURL URLWithString:url]] resume];
}
#else
void RLMSendAnalytics() {}
#endif

View File

@ -3,6 +3,7 @@
*/
#import "RealmReact.h"
#import "RealmAnalytics.h"
#import "RCTBridge.h"
#import "js_init.h"
@ -79,6 +80,14 @@ extern "C" JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executo
}
}
+ (void)initialize {
if (self != [RealmReact class]) {
return;
}
RLMSendAnalytics();
}
+ (NSString *)moduleName {
return @"Realm";
}