Repository navigation
[CDX-566] Migrate to v2 search submit endpoint #286
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
Changes from all commits
f37be95
0d94046
82b5f38
66a567f
0940749
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,27 +14,46 @@ import Foundation | |
| struct CIOTrackSearchSubmitData: CIORequestData { | ||
|
|
||
| let searchTerm: String | ||
| let originalQuery: String | ||
| let group: CIOGroup? | ||
| let userInput: String | ||
| let filters: CIOTrackSearchSubmitFilters? | ||
| let analyticsTags: [String: String]? | ||
| let section: String? | ||
|
evanyan13 marked this conversation as resolved.
|
||
|
|
||
| func url(with baseURL: String) -> String { | ||
| return String(format: Constants.TrackSearchSubmit.format, baseURL, self.searchTerm.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!) | ||
| return String(format: Constants.TrackSearchSubmit.format, baseURL) | ||
| } | ||
|
|
||
| init(searchTerm: String, originalQuery: String, group: CIOGroup? = nil, analyticsTags: [String: String]? = nil) { | ||
| init(searchTerm: String, userInput: String, filters: CIOTrackSearchSubmitFilters? = nil, analyticsTags: [String: String]? = nil, section: String? = nil) { | ||
| self.searchTerm = searchTerm | ||
| self.originalQuery = originalQuery | ||
| self.group = group | ||
| self.userInput = userInput | ||
| self.filters = filters | ||
| self.analyticsTags = analyticsTags | ||
| self.section = section | ||
| } | ||
|
|
||
| func decorateRequest(requestBuilder: RequestBuilder) { | ||
| requestBuilder.set(originalQuery: self.originalQuery) | ||
| if let group = self.group { | ||
| requestBuilder.set(groupName: group.displayName) | ||
| requestBuilder.set(groupID: group.groupID) | ||
| requestBuilder.set(autocompleteSection: self.section) | ||
| } | ||
|
|
||
| func httpMethod() -> String { | ||
| return "POST" | ||
| } | ||
|
|
||
| // The endpoint rejects unknown body parameters, so fields in the baseParams are not included in the body | ||
| func httpBody(baseParams _: [String: Any]) -> Data? { | ||
| var dict = [ | ||
| "search_term": self.searchTerm, | ||
| "user_input": self.userInput | ||
| ] as [String: Any] | ||
|
|
||
| if let filters = self.filters { | ||
| dict["filters"] = ["group_id": filters.groupID] | ||
| } | ||
|
|
||
| if let analyticsTags = self.analyticsTags { | ||
| dict["analytics_tags"] = analyticsTags | ||
| } | ||
| requestBuilder.set(analyticsTags: self.analyticsTags) | ||
|
|
||
| return try? JSONSerialization.data(withJSONObject: dict) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Important Issue: Every other v2 POST data struct (e.g. This struct deliberately skips that merge and explains the reason in the comment above ( More importantly, the existing comment (
constructor-claude-bedrock[bot] marked this conversation as resolved.
evanyan13 marked this conversation as resolved.
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // | ||
| // CIOTrackSearchSubmitFilters.swift | ||
| // AutocompleteClient | ||
| // | ||
| // Copyright (c) Constructor.io Corporation. All rights reserved. | ||
| // http://constructor.io/ | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| /** | ||
| Struct encapsulating the filters that can be sent when tracking a search submission. | ||
| Only `group_id` is supported for now. Using a struct keeps the API forward-compatible if more filters are added. | ||
| */ | ||
| public struct CIOTrackSearchSubmitFilters { | ||
|
evanyan13 marked this conversation as resolved.
|
||
| /** | ||
| The id of the group applied by the search. Should be present when the user selects a search suggestion that automatically applies a group filter. Must not be empty, the endpoint rejects empty values | ||
| */ | ||
| public let groupID: String | ||
|
evanyan13 marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| Create search submit filters. | ||
|
|
||
| - Parameters: | ||
| - groupID: The id of the group applied by the search | ||
| */ | ||
| public init(groupID: String) { | ||
| self.groupID = groupID | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // | ||
| // CIOTrackSearchSubmitRequest.swift | ||
| // AutocompleteClient | ||
| // | ||
| // Copyright (c) Constructor.io Corporation. All rights reserved. | ||
| // http://constructor.io/ | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| /** | ||
| Struct encapsulating the parameters that can be passed to constructorIO.trackSearchSubmit() function. | ||
| */ | ||
| public struct CIOTrackSearchSubmitRequest { | ||
|
|
||
| /** | ||
| The term that the user searched for | ||
| */ | ||
| public let searchTerm: String | ||
|
|
||
| /** | ||
| The current text in the input field | ||
| */ | ||
| public let userInput: String | ||
|
|
||
| /** | ||
| The filters applied by the search. Only a group id is supported, i.e. "Pumpkin in Canned Goods" | ||
| */ | ||
| public let filters: CIOTrackSearchSubmitFilters? | ||
|
|
||
| /** | ||
| Additional custom analytics tags to be sent with the event. Merged with the default analytics tags | ||
| */ | ||
| public let analyticsTags: [String: String]? | ||
|
|
||
| /** | ||
| The section of the index to use (defaults to the configured default section, or "Products") | ||
| */ | ||
| public let section: String? | ||
|
|
||
| /** | ||
| Create a search submit request | ||
|
|
||
| - Parameters: | ||
| - searchTerm: The term that the user searched for | ||
| - userInput: The current text in the input field | ||
| - filters: The filters applied by the search | ||
| - analyticsTags: Additional custom analytics tags to be sent with the event | ||
| - section: The section of the index to use | ||
| */ | ||
| public init(searchTerm: String, userInput: String, filters: CIOTrackSearchSubmitFilters? = nil, analyticsTags: [String: String]? = nil, section: String? = nil) { | ||
| self.searchTerm = searchTerm | ||
| self.userInput = userInput | ||
| self.filters = filters | ||
| self.analyticsTags = analyticsTags | ||
| self.section = section | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.