diff --git a/Example/Example/SearchResultsCollectionViewController.swift b/Example/Example/SearchResultsCollectionViewController.swift index 70e765a..de628a1 100644 --- a/Example/Example/SearchResultsCollectionViewController.swift +++ b/Example/Example/SearchResultsCollectionViewController.swift @@ -84,7 +84,7 @@ class SearchResultsCollectionViewController: UICollectionViewController { // call tracking api here to register the click // alternately, you can present similar images or recommend them to users in the image detail page - let params = ViTrackParams(accessKey: ViSearch.sharedInstance.client!.accessKey, reqId: self.reqId, action: "click") + let params = ViTrackParams(reqId: self.reqId, action: "click") params?.imName = im_name ViSearch.sharedInstance.track(params: params!) { (success, error) in diff --git a/README.md b/README.md index 3632c44..3591ecc 100644 --- a/README.md +++ b/README.md @@ -549,7 +549,7 @@ User action (e.g. click on an image after search) can be sent in this way: ```swift -let params = ViTrackParams(accessKey: ViSearch.sharedInstance.client!.accessKey, reqId: recentReqId, action: "click" ) +let params = ViTrackParams(reqId: recentReqId, action: "click" ) // You can also append an im_name field params.imName = "example_clicked_im_name" diff --git a/ViSearchSDK/ViSearchSDK/Classes/Request/ViTrackParams.swift b/ViSearchSDK/ViSearchSDK/Classes/Request/ViTrackParams.swift index 81498dc..a3f8139 100644 --- a/ViSearchSDK/ViSearchSDK/Classes/Request/ViTrackParams.swift +++ b/ViSearchSDK/ViSearchSDK/Classes/Request/ViTrackParams.swift @@ -8,16 +8,14 @@ public class ViTrackParams : ViSearchParamsProtocol{ public var imName : String? public var reqId : String public var cuid : String? - var cid : String - - //MARK: init + var cid : String? /// Init tracking parameters. For example, after user search for images, we want to track which image they clicked /// /// - parameter accessKey: API access key /// - parameter reqId: recent request id e.g. search for similar images /// - parameter action: tracking action e.g. click, add to cart, etc. Currently only click is supported - public init?(accessKey: String, reqId : String , action: String ) { + public convenience init?(accessKey: String, reqId : String , action: String ) { if accessKey.isEmpty { print("\(type(of: self)).\(#function)[line:\(#line)] - error: accessKey parameter is missing") @@ -25,23 +23,30 @@ public class ViTrackParams : ViSearchParamsProtocol{ return nil } + self.init(reqId: reqId, action: action) + + self.cid = accessKey + self.cuid = nil + self.imName = nil + } + + /// Init tracking parameters. For example, after user search for images, we want to track which image they clicked + /// Assumption: access key will be passed by client later + /// - parameter reqId: recent request id e.g. search for similar images + /// - parameter action: tracking action e.g. click, add to cart, etc. Currently only click is supported + public init?(reqId : String , action: String){ if reqId.isEmpty { print("\(type(of: self)).\(#function)[line:\(#line)] - error: reqId parameter is missing") - return nil } if action.isEmpty { print("\(type(of: self)).\(#function)[line:\(#line)] - error: action parameter is missing") - return nil } - - self.cid = accessKey + self.reqId = reqId self.action = action - self.cuid = nil - self.imName = nil } //MARK: search protocol diff --git a/ViSearchSDK/ViSearchSDK/Classes/ViSearch.swift b/ViSearchSDK/ViSearchSDK/Classes/ViSearch.swift index 8fb9630..c480205 100644 --- a/ViSearchSDK/ViSearchSDK/Classes/ViSearch.swift +++ b/ViSearchSDK/ViSearchSDK/Classes/ViSearch.swift @@ -92,7 +92,7 @@ open class ViSearch: NSObject { return nil } - // track the API calls and various actions + /// track the API calls and various actions /// Tracking API @discardableResult public func track(params: ViTrackParams, handler: ( (_ success: Bool, Error?) -> Void )? diff --git a/ViSearchSDK/ViSearchSDK/Classes/ViSearchClient.swift b/ViSearchSDK/ViSearchSDK/Classes/ViSearchClient.swift index 0a68826..2bbfbb2 100644 --- a/ViSearchSDK/ViSearchSDK/Classes/ViSearchClient.swift +++ b/ViSearchSDK/ViSearchSDK/Classes/ViSearchClient.swift @@ -149,6 +149,8 @@ open class ViSearchClient: NSObject, URLSessionDelegate { handler: ( (_ success: Bool, Error?) -> Void )? ) -> Void { + params.cid = accessKey + // different url for tracking let url = requestSerialization.generateRequestUrl(baseUrl: trackUrl , apiEndPoint: .TRACK , searchParams: params) let request = NSMutableURLRequest(url: URL(string: url)! , cachePolicy: .useProtocolCachePolicy , timeoutInterval: timeoutInterval)