Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Oct 27, 2023
1 parent 625163b commit 9e12d79
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 64 deletions.
4 changes: 4 additions & 0 deletions PostHog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
3AE3FB4B2993A68500AFFC18 /* StorageTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE3FB4A2993A68500AFFC18 /* StorageTest.swift */; };
3AE3FB4E2993D1D600AFFC18 /* PostHogSessionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE3FB4D2993D1D600AFFC18 /* PostHogSessionManager.swift */; };
690FF05F2AE7E2D400A0B06B /* Data+Gzip.swift in Sources */ = {isa = PBXBuildFile; fileRef = 690FF05E2AE7E2D400A0B06B /* Data+Gzip.swift */; };
690FF0B52AEBBD3C00A0B06B /* DictUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 690FF0B42AEBBD3C00A0B06B /* DictUtils.swift */; };
69261D132AD5685B00232EC7 /* PostHogFeatureFlags.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69261D122AD5685B00232EC7 /* PostHogFeatureFlags.swift */; };
69261D192AD9673500232EC7 /* PostHogBatchUploadInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69261D182AD9673500232EC7 /* PostHogBatchUploadInfo.swift */; };
69261D1B2AD9678C00232EC7 /* PostHogEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69261D1A2AD9678C00232EC7 /* PostHogEvent.swift */; };
Expand Down Expand Up @@ -168,6 +169,7 @@
690FF02F2AE7C5BA00A0B06B /* PostHogExampleWithPods.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = PostHogExampleWithPods.xcodeproj; path = PostHogExampleWithPods/PostHogExampleWithPods.xcodeproj; sourceTree = "<group>"; };
690FF0532AE7DB3700A0B06B /* PostHogExampleWithSPM.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = PostHogExampleWithSPM.xcodeproj; path = PostHogExampleWithSPM/PostHogExampleWithSPM.xcodeproj; sourceTree = "<group>"; };
690FF05E2AE7E2D400A0B06B /* Data+Gzip.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Gzip.swift"; sourceTree = "<group>"; };
690FF0B42AEBBD3C00A0B06B /* DictUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DictUtils.swift; sourceTree = "<group>"; };
69261D122AD5685B00232EC7 /* PostHogFeatureFlags.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogFeatureFlags.swift; sourceTree = "<group>"; };
69261D182AD9673500232EC7 /* PostHogBatchUploadInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogBatchUploadInfo.swift; sourceTree = "<group>"; };
69261D1A2AD9678C00232EC7 /* PostHogEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogEvent.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -279,6 +281,7 @@
3A0F108429C9ABB6002C0084 /* ReadWriteLock.swift */,
3A0F108829C9BD76002C0084 /* Errors.swift */,
690FF05E2AE7E2D400A0B06B /* Data+Gzip.swift */,
690FF0B42AEBBD3C00A0B06B /* DictUtils.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -642,6 +645,7 @@
3AE3FB3D29924E8200AFFC18 /* PostHogSDK.swift in Sources */,
3AE3FB3F29924F4F00AFFC18 /* PostHogConfig.swift in Sources */,
3AE3FB332991388500AFFC18 /* PostHogQueue.swift in Sources */,
690FF0B52AEBBD3C00A0B06B /* DictUtils.swift in Sources */,
69261D1B2AD9678C00232EC7 /* PostHogEvent.swift in Sources */,
3AE3FB472992AB0000AFFC18 /* Hedgelog.swift in Sources */,
69261D132AD5685B00232EC7 /* PostHogFeatureFlags.swift in Sources */,
Expand Down
11 changes: 0 additions & 11 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -733,15 +733,4 @@ let maxRetryDelay = 30.0
}
capture("Application Backgrounded")
}

private func sanitizeDicionary(_ dict: [String: Any]?) -> [String: Any]? {
if dict == nil || dict!.isEmpty {
return nil
}
if !JSONSerialization.isValidJSONObject(dict!) {
hedgeLog("Properties: \(dict!) are not serializable, dropping those items.")
}

return dict
}
}
43 changes: 43 additions & 0 deletions PostHog/Utils/DictUtils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// DictUtils.swift
// PostHog
//
// Created by Manoel Aranda Neto on 27.10.23.
//

import Foundation

public func sanitizeDicionary(_ dict: [String: Any]?) -> [String: Any]? {
if dict == nil || dict!.isEmpty {
return nil
}

var newDict = dict!

for (key, value) in newDict where !isValidObject(value) {
if value is URL {
newDict[key] = (value as! URL).absoluteString
continue
}
if value is Date {
newDict[key] = ISO8601DateFormatter().string(from: (value as! Date))
continue
}

newDict.removeValue(forKey: key)
hedgeLog("property: \(key) isn't serializable, dropping the item")
}

return newDict
}

private func isValidObject(_ object: Any) -> Bool {
if object is String || object is Bool || object is any Numeric || object is NSNumber {
return true
}
if object is [Any?] || object is [String: Any?] {
return JSONSerialization.isValidJSONObject(object)
}
// workaround [object] since isValidJSONObject only accepts an Array or Dict
return JSONSerialization.isValidJSONObject([object])
}
12 changes: 9 additions & 3 deletions PostHogExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ class AppDelegate: NSObject, UIApplicationDelegate {

PostHogSDK.shared.setup(config)
// PostHogSDK.shared.debug()
// PostHogSDK.shared.capture("App started!")
// let props: [String : Any] = ["test": 1,
// "test2": 2.1,
// "test3": "test",
// "test4": true,
// "test5": [1, 2, 3],
// "test6": ["one": 1]]
let defaultCenter = NotificationCenter.default
let props: [String: Any] = ["not": defaultCenter, "url": URL(string: "123")!]
PostHogSDK.shared.capture("App started!", properties: props)

// DispatchQueue.global(qos: .utility).async {
// let task = Api().failingRequest()
Expand All @@ -29,8 +37,6 @@ class AppDelegate: NSObject, UIApplicationDelegate {
// ])
// }

let defaultCenter = NotificationCenter.default

#if os(iOS) || os(tvOS)
defaultCenter.addObserver(self,
selector: #selector(receiveFeatureFlags),
Expand Down
113 changes: 63 additions & 50 deletions PostHogObjCExample/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,60 +31,73 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
config.preloadFeatureFlags = NO;
[[PostHogSDK shared] debug:YES];
[[PostHogSDK shared] setup:config];
NSLog(@"getDistinctId: %@", [[PostHogSDK shared] getDistinctId]);
NSLog(@"getAnonymousId: %@", [[PostHogSDK shared] getAnonymousId]);

// NSLog(@"getDistinctId: %@", [[PostHogSDK shared] getDistinctId]);
// NSLog(@"getAnonymousId: %@", [[PostHogSDK shared] getAnonymousId]);
//
NSMutableDictionary *props = [NSMutableDictionary dictionary];
props[@"state"] = @"running";

NSMutableDictionary *userProps = [NSMutableDictionary dictionary];
userProps[@"userAge"] = @50;

NSMutableDictionary *userPropsOnce = [NSMutableDictionary dictionary];
userPropsOnce[@"userAlive"] = @YES;

NSMutableDictionary *groupProps = [NSMutableDictionary dictionary];
groupProps[@"groupName"] = @"theGroup";
props[@"str"] = @"running";
props[@"numb"] = @1;
props[@"numb_2"] = @1.5;
props[@"bool"] = @YES;
props[@"date"] = NSDate.now;
props[@"url"] = [NSURL URLWithString:@"test"];
NSMutableArray *doubleDigits = [NSMutableArray array];
[doubleDigits addObject:@1];
props[@"arr"] = doubleDigits;
NSMutableDictionary *props2 = [NSMutableDictionary dictionary];
props2[@"1"] = @"test";
props[@"dict"] = props2;

NSMutableDictionary *registerProps = [NSMutableDictionary dictionary];
props[@"loggedIn"] = @YES;
[[PostHogSDK shared] registerProperties:registerProps];
[[PostHogSDK shared] unregisterProperties:@"test2"];

[[PostHogSDK shared] identify:@"my_new_id"];
[[PostHogSDK shared] identifyWithDistinctId:@"my_new_id" userProperties:userProps];
[[PostHogSDK shared] identifyWithDistinctId:@"my_new_id" userProperties:userProps userPropertiesSetOnce:userPropsOnce];


[[PostHogSDK shared] optIn];
[[PostHogSDK shared] optOut];
NSLog(@"isOptOut: %d", [[PostHogSDK shared] isOptOut]);
NSLog(@"isFeatureEnabled: %d", [[PostHogSDK shared] isFeatureEnabled:@"myFlag"]);
NSLog(@"getFeatureFlag: %@", [[PostHogSDK shared] getFeatureFlag:@"myFlag"]);
NSLog(@"getFeatureFlagPayload: %@", [[PostHogSDK shared] getFeatureFlagPayload:@"myFlag"]);

[[PostHogSDK shared] reloadFeatureFlags];
[[PostHogSDK shared] reloadFeatureFlagsWithCallback:^(){
NSLog(@"called");
}];

[[PostHogSDK shared] capture:@"theEvent"];
//
// NSMutableDictionary *userProps = [NSMutableDictionary dictionary];
// userProps[@"userAge"] = @50;
//
// NSMutableDictionary *userPropsOnce = [NSMutableDictionary dictionary];
// userPropsOnce[@"userAlive"] = @YES;
//
// NSMutableDictionary *groupProps = [NSMutableDictionary dictionary];
// groupProps[@"groupName"] = @"theGroup";
//
// NSMutableDictionary *registerProps = [NSMutableDictionary dictionary];
// props[@"loggedIn"] = @YES;
// [[PostHogSDK shared] registerProperties:registerProps];
// [[PostHogSDK shared] unregisterProperties:@"test2"];
//
// [[PostHogSDK shared] identify:@"my_new_id"];
// [[PostHogSDK shared] identifyWithDistinctId:@"my_new_id" userProperties:userProps];
// [[PostHogSDK shared] identifyWithDistinctId:@"my_new_id" userProperties:userProps userPropertiesSetOnce:userPropsOnce];
//
//
// [[PostHogSDK shared] optIn];
// [[PostHogSDK shared] optOut];
// NSLog(@"isOptOut: %d", [[PostHogSDK shared] isOptOut]);
// NSLog(@"isFeatureEnabled: %d", [[PostHogSDK shared] isFeatureEnabled:@"myFlag"]);
// NSLog(@"getFeatureFlag: %@", [[PostHogSDK shared] getFeatureFlag:@"myFlag"]);
// NSLog(@"getFeatureFlagPayload: %@", [[PostHogSDK shared] getFeatureFlagPayload:@"myFlag"]);
//
// [[PostHogSDK shared] reloadFeatureFlags];
// [[PostHogSDK shared] reloadFeatureFlagsWithCallback:^(){
// NSLog(@"called");
// }];
//
// [[PostHogSDK shared] capture:@"theEvent"];
[[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props];
[[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props userProperties:userProps];
[[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props userProperties:userProps userPropertiesSetOnce:userPropsOnce];
[[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props userProperties:userProps userPropertiesSetOnce:userPropsOnce groupProperties:groupProps];

[[PostHogSDK shared] groupWithType:@"theType" key:@"theKey"];
[[PostHogSDK shared] groupWithType:@"theType" key:@"theKey" groupProperties:groupProps];

[[PostHogSDK shared] alias:@"theAlias"];

[[PostHogSDK shared] screen:@"theScreen"];
[[PostHogSDK shared] screenWithTitle:@"theScreen" properties:props];

[[PostHogSDK shared] flush];
[[PostHogSDK shared] reset];
[[PostHogSDK shared] close];
// [[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props userProperties:userProps];
// [[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props userProperties:userProps userPropertiesSetOnce:userPropsOnce];
// [[PostHogSDK shared] captureWithEvent:@"theEvent" properties:props userProperties:userProps userPropertiesSetOnce:userPropsOnce groupProperties:groupProps];
//
// [[PostHogSDK shared] groupWithType:@"theType" key:@"theKey"];
// [[PostHogSDK shared] groupWithType:@"theType" key:@"theKey" groupProperties:groupProps];
//
// [[PostHogSDK shared] alias:@"theAlias"];
//
// [[PostHogSDK shared] screen:@"theScreen"];
// [[PostHogSDK shared] screenWithTitle:@"theScreen" properties:props];
//
// [[PostHogSDK shared] flush];
// [[PostHogSDK shared] reset];
// [[PostHogSDK shared] close];

PostHogSDK *postHog = [PostHogSDK with:config];

Expand Down

0 comments on commit 9e12d79

Please sign in to comment.