@@ -66,18 +66,20 @@ final public class SimpleAnalytics: NSObject {
66
66
67
67
/// Track a pageview
68
68
/// - Parameter path: The path of the page as string array, for example: `["list", "detailview", "edit"]`
69
- public func track( path: [ String ] ) {
70
- self . trackPageView ( path: pathToString ( path: path) )
69
+ /// - Parameter metadata: An optional dictionary of metadata to be sent with the pageview. `["plan": "premium", "referrer": "landing_page"]`
70
+ public func track( path: [ String ] , metadata: [ String : Any ] ? = nil ) {
71
+ self . trackPageView ( path: pathToString ( path: path) , metadata: metadataToJsonString ( metadata: metadata) )
71
72
}
72
73
73
74
/// Track an event
74
75
/// - Parameter event: The event name
75
76
/// - Parameter path: optional path array where the event took place, for example: `["list", "detailview", "edit"]`
76
- public func track( event: String , path: [ String ] = [ ] ) {
77
- self . trackEvent ( event: event, path: pathToString ( path: path) )
77
+ /// - Parameter metadata: An optional dictionary of metadata to be sent with the pageview. `["plan": "premium", "referrer": "landing_page"]`
78
+ public func track( event: String , path: [ String ] = [ ] , metadata: [ String : Any ] ? = nil ) {
79
+ self . trackEvent ( event: event, path: pathToString ( path: path) , metadata: metadataToJsonString ( metadata: metadata) )
78
80
}
79
81
80
- internal func trackPageView( path: String ) {
82
+ internal func trackPageView( path: String , metadata : String ? = nil ) {
81
83
guard !isOptedOut else {
82
84
return
83
85
}
@@ -89,13 +91,14 @@ final public class SimpleAnalytics: NSObject {
89
91
path: path,
90
92
language: userLanguage,
91
93
timezone: userTimezone,
92
- unique: isUnique ( )
94
+ unique: isUnique ( ) ,
95
+ metadata: metadata
93
96
)
94
97
95
98
RequestDispatcher . sendEventRequest ( event: event)
96
99
}
97
100
98
- internal func trackEvent( event: String , path: String = " " ) {
101
+ internal func trackEvent( event: String , path: String = " " , metadata : String ? = nil ) {
99
102
guard !isOptedOut else {
100
103
return
101
104
}
@@ -107,7 +110,8 @@ final public class SimpleAnalytics: NSObject {
107
110
path: path,
108
111
language: userLanguage,
109
112
timezone: userTimezone,
110
- unique: isUnique ( )
113
+ unique: isUnique ( ) ,
114
+ metadata: metadata
111
115
)
112
116
RequestDispatcher . sendEventRequest ( event: event)
113
117
}
@@ -125,6 +129,21 @@ final public class SimpleAnalytics: NSObject {
125
129
return " / \( safePath. joined ( separator: " / " ) ) "
126
130
}
127
131
132
+ /// Serializes metadata dictionary into a JSON string.
133
+ /// - Parameter metadata: The metadata dictionary, which is optional.
134
+ /// - Returns: A JSON string representation of the metadata or nil if serialization fails or metadata is nil/empty.
135
+ internal func metadataToJsonString( metadata: [ String : Any ] ? ) -> String ? {
136
+ guard let metadata = metadata, !metadata. isEmpty else { return nil }
137
+
138
+ do {
139
+ let data = try JSONSerialization . data ( withJSONObject: metadata, options: [ ] )
140
+ return String ( data: data, encoding: . utf8)
141
+ } catch {
142
+ print ( " Error serializing metadata to JSON string: \( error) " )
143
+ return nil
144
+ }
145
+ }
146
+
128
147
/// Simple Analytics uses the `isUnique` flag to determine visitors from pageviews. The first event/pageview for the day
129
148
/// should get this `isUnique` flag.
130
149
/// - Returns: if this is a unique first visit for today
0 commit comments