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

feat: headers and query item capability added to proxy server #1

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
918d3ad
headers and query item capability added to proxy server
mohitanand-cred Dec 12, 2023
91c16b9
added header capability to other network requests
mohitanand-cred Dec 21, 2023
488edd8
automatically collecting common mobile events is a legacy feature so …
zihejia Jan 9, 2024
ec16672
Prevent crashes in unregisterSuperProperty
Jan 16, 2024
618d245
fix CI
zihejia Jan 17, 2024
a8c862f
Merge pull request #624 from mixpanel/zihe-fix-CI-flake
zihejia Jan 17, 2024
ffd0d35
Merge pull request #623 from daleswift/prevent-crash-unregisterSuperP…
zihejia Jan 17, 2024
5b375ea
Automatic Props
ay8s Jan 30, 2024
0f10d84
Import UIKit
ay8s Jan 30, 2024
3750264
Remoev print
ay8s Jan 30, 2024
9438b4a
Merge pull request #627 from ay8s/task/visionos
zihejia Feb 2, 2024
c432bc8
added comment for macos initialize function and separated MixpanelIns…
mohitanand-cred Feb 9, 2024
324d59f
add privacy manifest
jaredmixpanel Mar 14, 2024
e1d64e7
Merge pull request #633 from mixpanel/jared-add-privacy-manifest
jaredmixpanel Mar 14, 2024
5ba0816
Version 4.2.1
jaredmixpanel Mar 14, 2024
60e265a
Update docs
jaredmixpanel Mar 14, 2024
4fa0f5d
Update CHANGELOG
zihejia Mar 14, 2024
8c9b38a
move privacy manifest
jaredmixpanel Mar 18, 2024
6f0a966
Merge pull request #634 from mixpanel/jared-move-privacy-manifest
jaredmixpanel Mar 18, 2024
9f4de4f
Version 4.2.2
jaredmixpanel Mar 18, 2024
e133990
Update docs
jaredmixpanel Mar 18, 2024
09f6555
Update CHANGELOG
zihejia Mar 18, 2024
1aae2cc
make privacy manifest a resource
jaredmixpanel Mar 25, 2024
0f021d9
add to Copy Bundle Resources
jaredmixpanel Mar 25, 2024
6c592db
Merge pull request #636 from mixpanel/jared-privacy-resource
jaredmixpanel Mar 25, 2024
9450536
Version 4.2.3
jaredmixpanel Mar 25, 2024
f9c08fa
Update docs
jaredmixpanel Mar 25, 2024
fe2ed53
Update CHANGELOG
zihejia Mar 25, 2024
aeb303f
use resources_bundle in podspec
jaredmixpanel Mar 27, 2024
bf6c6f7
Merge pull request #638 from mixpanel/jared-pod-resources-bundle
jaredmixpanel Mar 27, 2024
8cc55a6
Version 4.2.4
jaredmixpanel Mar 27, 2024
c3be71e
Update docs
jaredmixpanel Mar 27, 2024
e84dcf7
Update CHANGELOG
zihejia Mar 27, 2024
68ea193
Merge branch 'master' of https://github.com/mixpanel/mixpanel-swift i…
mohitanand-cred Apr 7, 2024
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
13 changes: 8 additions & 5 deletions Sources/Flush.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ class Flush: AppLifecycle {
flushIntervalReadWriteLock = DispatchQueue(label: "com.mixpanel.flush_interval.lock", qos: .utility, attributes: .concurrent, autoreleaseFrequency: .workItem)
}

func flushQueue(_ queue: Queue, type: FlushType) {
func flushQueue(_ queue: Queue, type: FlushType, headers: [String: String], queryItems: [URLQueryItem]) {
if flushRequest.requestNotAllowed() {
return
}
flushQueueInBatches(queue, type: type)
flushQueueInBatches(queue, type: type, headers: headers, queryItems: queryItems)
}

func startFlushTimer() {
Expand Down Expand Up @@ -95,7 +95,7 @@ class Flush: AppLifecycle {
}
}

func flushQueueInBatches(_ queue: Queue, type: FlushType) {
func flushQueueInBatches(_ queue: Queue, type: FlushType, headers: [String: String], queryItems: [URLQueryItem]) {
var mutableQueue = queue
while !mutableQueue.isEmpty {
let batchSize = min(mutableQueue.count, flushBatchSize)
Expand All @@ -115,8 +115,10 @@ class Flush: AppLifecycle {
}
#endif // os(iOS)
let success = flushRequest.sendRequest(requestData,
type: type,
useIP: useIPAddressForGeoLocation)
type: type,
useIP: useIPAddressForGeoLocation,
headers: headers,
queryItems: queryItems)
#if os(iOS)
if !MixpanelInstance.isiOSAppExtension() {
delegate?.updateNetworkActivityIndicator(false)
Expand Down Expand Up @@ -156,3 +158,4 @@ class Flush: AppLifecycle {
}

}

13 changes: 10 additions & 3 deletions Sources/FlushRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class FlushRequest: Network {

func sendRequest(_ requestData: String,
type: FlushType,
useIP: Bool) -> Bool {
useIP: Bool,
headers: [String: String],
queryItems: [URLQueryItem] = []) -> Bool {

let responseParser: (Data) -> Int? = { data in
let response = String(data: data, encoding: String.Encoding.utf8)
Expand All @@ -31,12 +33,16 @@ class FlushRequest: Network {
return nil
}

let resourceHeaders: [String: String] = ["Content-Type": "application/json"].merging(headers) {(_,new) in new }

let ipString = useIP ? "1" : "0"
var resourceQueryItems: [URLQueryItem] = [URLQueryItem(name: "ip", value: ipString)]
resourceQueryItems.append(contentsOf: queryItems)
let resource = Network.buildResource(path: type.rawValue,
method: .post,
requestBody: requestData.data(using: .utf8),
queryItems: [URLQueryItem(name: "ip", value: ipString)],
headers: ["Content-Type": "application/json"],
queryItems: resourceQueryItems,
headers: resourceHeaders,
parse: responseParser)
var result = false
let semaphore = DispatchSemaphore(value: 0)
Expand Down Expand Up @@ -97,3 +103,4 @@ class FlushRequest: Network {
}

}

129 changes: 121 additions & 8 deletions Sources/Mixpanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,49 @@ open class Mixpanel {
superProperties: superProperties,
serverURL: serverURL)
}

/**
Initializes an instance of the API with the given project token.

Returns a new Mixpanel instance API object. This allows you to create more than one instance
of the API object, which is convenient if you'd like to send data to more than
one Mixpanel project from a single app.

- parameter token: your project token
- parameter trackAutomaticEvents: Whether or not to collect common mobile events
- parameter flushInterval: Optional. Interval to run background flushing
- parameter instanceName: Optional. The name you want to uniquely identify the Mixpanel Instance.
It is useful when you want more than one Mixpanel instance under the same project token.
- parameter optOutTrackingByDefault: Optional. Whether or not to be opted out from tracking by default
- parameter useUniqueDistinctId: Optional. whether or not to use the unique device identifier as the distinct_id
- parameter superProperties: Optional. Super properties dictionary to register during initialization
- parameter proxyServerConfig: Optional. Setup for proxy server.

- important: If you have more than one Mixpanel instance, it is beneficial to initialize
the instances with an instanceName. Then they can be reached by calling getInstance with name.

- returns: returns a mixpanel instance if needed to keep throughout the project.
You can always get the instance by calling getInstance(name)
*/

@discardableResult
open class func initialize(token apiToken: String,
trackAutomaticEvents: Bool,
flushInterval: Double = 60,
instanceName: String? = nil,
optOutTrackingByDefault: Bool = false,
useUniqueDistinctId: Bool = false,
superProperties: Properties? = nil,
proxyServerConfig: ProxyServerConfig) -> MixpanelInstance {
return MixpanelManager.sharedInstance.initialize(token: apiToken,
flushInterval: flushInterval,
instanceName: ((instanceName != nil) ? instanceName! : apiToken),
trackAutomaticEvents: trackAutomaticEvents,
optOutTrackingByDefault: optOutTrackingByDefault,
useUniqueDistinctId: useUniqueDistinctId,
superProperties: superProperties,
proxyServerConfig: proxyServerConfig)
}
#else
/**
Initializes an instance of the API with the given project token (MAC OS ONLY).
Expand Down Expand Up @@ -97,6 +140,47 @@ open class Mixpanel {
superProperties: superProperties,
serverURL: serverURL)
}

/**
Initializes an instance of the API with the given project token (MAC OS ONLY).

Returns a new Mixpanel instance API object. This allows you to create more than one instance
of the API object, which is convenient if you'd like to send data to more than
one Mixpanel project from a single app.

- parameter token: your project token
- parameter flushInterval: Optional. Interval to run background flushing
- parameter instanceName: Optional. The name you want to uniquely identify the Mixpanel Instance.
It is useful when you want more than one Mixpanel instance under the same project token.
- parameter optOutTrackingByDefault: Optional. Whether or not to be opted out from tracking by default
- parameter useUniqueDistinctId: Optional. whether or not to use the unique device identifier as the distinct_id
- parameter superProperties: Optional. Super properties dictionary to register during initialization
- parameter proxyServerConfig: Optional. Setup for proxy server.

- important: If you have more than one Mixpanel instance, it is beneficial to initialize
the instances with an instanceName. Then they can be reached by calling getInstance with name.

- returns: returns a mixpanel instance if needed to keep throughout the project.
You can always get the instance by calling getInstance(name)
*/

@discardableResult
open class func initialize(token apiToken: String,
flushInterval: Double = 60,
instanceName: String? = nil,
optOutTrackingByDefault: Bool = false,
useUniqueDistinctId: Bool = false,
superProperties: Properties? = nil,
proxyServerConfig: ProxyServerConfig) -> MixpanelInstance {
return MixpanelManager.sharedInstance.initialize(token: apiToken,
flushInterval: flushInterval,
instanceName: ((instanceName != nil) ? instanceName! : apiToken),
trackAutomaticEvents: false,
optOutTrackingByDefault: optOutTrackingByDefault,
useUniqueDistinctId: useUniqueDistinctId,
superProperties: superProperties,
proxyServerConfig: proxyServerConfig)
}
#endif // os(OSX)

/**
Expand Down Expand Up @@ -178,20 +262,48 @@ final class MixpanelManager {
superProperties: Properties? = nil,
serverURL: String? = nil
) -> MixpanelInstance {
return dequeueInstance(instanceName: instanceName) {
return MixpanelInstance(apiToken: apiToken,
flushInterval: flushInterval,
name: instanceName,
trackAutomaticEvents: trackAutomaticEvents,
optOutTrackingByDefault: optOutTrackingByDefault,
useUniqueDistinctId: useUniqueDistinctId,
superProperties: superProperties,
serverURL: serverURL)
}
}

func initialize(token apiToken: String,
flushInterval: Double,
instanceName: String,
trackAutomaticEvents: Bool,
optOutTrackingByDefault: Bool = false,
useUniqueDistinctId: Bool = false,
superProperties: Properties? = nil,
proxyServerConfig: ProxyServerConfig
) -> MixpanelInstance {
return dequeueInstance(instanceName: instanceName) {
return MixpanelInstance(apiToken: apiToken,
flushInterval: flushInterval,
name: instanceName,
trackAutomaticEvents: trackAutomaticEvents,
optOutTrackingByDefault: optOutTrackingByDefault,
useUniqueDistinctId: useUniqueDistinctId,
superProperties: superProperties,
proxyServerConfig: proxyServerConfig)
}
}

private func dequeueInstance(instanceName: String, instanceCreation: () -> MixpanelInstance) -> MixpanelInstance {
instanceQueue.sync {
var instance: MixpanelInstance?
if let instance = instances[instanceName] {
mainInstance = instance
return
}
instance = MixpanelInstance(apiToken: apiToken,
flushInterval: flushInterval,
name: instanceName,
trackAutomaticEvents: trackAutomaticEvents,
optOutTrackingByDefault: optOutTrackingByDefault,
useUniqueDistinctId: useUniqueDistinctId,
superProperties: superProperties,
serverURL: serverURL)

instance = instanceCreation()
readWriteLock.write {
instances[instanceName] = instance!
mainInstance = instance!
Expand Down Expand Up @@ -245,3 +357,4 @@ final class MixpanelManager {
}

}

Loading