Constructor.io Swift 客户端
为 Constructor.io 提供的 iOS 客户端。Constructor.io 提供一种作为服务的搜索,它使用人工智能优化结果(包括自然语言处理、重新排序以优化转化率和用户个性化)。
文档
完整的 API 文档可在 Github Pages 上找到
1. 导入
1.a 使用 CocoaPods 进行导入
首先确保您已安装 CocoaPods。然后在项目根目录中创建一个名为“Podfile”的空文本文件。将该文件中添加以下行:
target ‘YOUR_TARGET_NAME’ do
pod ‘ConstructorAutocomplete'
end
在终端中打开(确保您位于项目根目录下)并输入:
pod install
1.b 使用 Carthage 进行导入
首先确保您已安装 Carthage。然后在项目根目录中创建一个名为“Cartfile”的空文本文件。在该文件中添加以下行:
github "Constructor-io/constructorio-client-swift"
在终端中打开(确保您位于项目根目录下)并输入:
carthage update
将 ConstructorIO.framework
从 Carthage/Build/iOS 拖动到您的项目中,并将其链接到应用程序目标。另外,确保通过添加一个新的“复制文件”阶段来复制框架。
2. 检索 API 密钥
您可以在您的 Constructor.io 控制台 中找到它。如果您想注册,请联系销售部门,如果您相信您的公司已经有了账户,请联系技术支持部门。
3. 创建客户端实例
确保在源文件顶部导入ConstructorAutocomplete
模块,然后写入以下内容:
// Create the client config
let config = ConstructorIOConfig(
apiKey: "YOUR API KEY",
baseURL: "ac.cnstrc.com", // default
resultCount: AutocompleteResultCount(numResultsForSection: ["Search Suggestions" : 3, "Products" : 0])
)
// Create the client instance
let constructorIO = ConstructorIO(config: config)
// Set the user ID (for a logged in user) used for cross device personalization
constructorIO.userID = "abcdefghijk-123"
4. 请求自动补全结果
let query = CIOAutocompleteQuery(query: "apple", numResultsForSection: ["Products": 6, "Search Suggestions": 8])
constructorIO.autocomplete(forQuery: query) { (response) in
let data = response.data!
let error = response.error!
// ...
}
5. 请求搜索结果
let filters = CIOQueryFilters(groupFilter: "Bread", facetFilters: [
(key: "Nutrition", value: "Organic"),
(key: "Nutrition", value: "Natural"),
(key: "Nutrition", value: "Whole-grain")
])
let query = CIOSearchQuery(query: "Dave's Bread", page: 5, filters: filters)
// Specify the sort order in which groups are returned
let groupsSortOption = CIOGroupsSortOption(sortBy: CIOGroupsSortBy.value, sortOrder: CIOGroupsSortOrder.ascending)
constructorIO.search(forQuery: query, filters: filters, groupsSortOption: groupsSortOption) { (response) in
let data = response.data!
let error = response.error!
// ...
}
6. 请求浏览结果
let query = CIOBrowseQuery(filterName: "potato", filterValue: "russet")
// Specify the sort order in which groups are returned
let groupsSortOption = CIOGroupsSortOption(sortBy: CIOGroupsSortBy.value, sortOrder: CIOGroupsSortOrder.ascending)
constructorIO.browse(forQuery: query, groupsSortOption: groupsSortOption) { (response) in
let data = response.data!
let error = response.error!
// ...
}
7. 请求推荐结果
let query = CIORecommendationsQuery(podId: "pdp_best_sellers", filters: filters)
constructorIO.recommendations(forQuery: query) { (response) in
let data = response.data!
let error = response.error!
// ...
}
具有替代/互补项目推荐策略的项ID
let itemId = "P18232"
let query = CIORecommendationsQuery(podId: "pdp_complementary_items", itemId: itemId)
constructorIO.recommendations(forQuery: query) { (response) in
let data = response.data!
let error = response.error!
// ...
}
具有过滤项目推荐策略的筛选器
let filters = CIOQueryFilters(groupFilter: "cat_1234", facetFilters: [
(key: "Nutrition", value: "Organic"),
(key: "Nutrition", value: "Natural"),
(key: "Brand", value: "Kroger")
])
let query = CIORecommendationsQuery(podId: "pdp_filtered_items", filters: filters)
constructorIO.recommendations(forQuery: query) { (response) in
let data = response.data!
let error = response.error!
// ...
}
8. 请求测验下一个问题
let query = CIOQuizQuery(quizId: "quiz-1", answers: [["1"], ["2"]])
constructorIO.getQuizNextQuestion(forQuery: query) { (response) in
let data = response.data!
let error = response.error!
// ...
}
9. 请求测验结果
let query = CIOQuizQuery(quizId: "quiz-1", answers: [["1"], ["2"]])
constructorIO.getQuizResults(forQuery: query) { (response) in
let data = response.data!
let error = response.error!
// ...
}
10. 仪器行为事件
iOS 客户端会将行为事件发送到 Constructor.io,以便持续学习和改进未来自动完成和搜索请求的结果。客户端仅在响应消耗者应用的调用或用户交互时发送事件。例如,如果消耗者应用从未调用 SDK 代码,则不会发送事件。除了显式传递的事件参数外,所有用户事件都包含一个客户端设置的基于 GUID 的用户 ID,用于标识用户以及会话 ID。
存在这三种类型的事件
- 通用事件在创建或初始化客户端实例时按需发送
- 自动完成事件衡量用户与自动完成结果的交互
- 搜索事件衡量用户与搜索结果的交互
- 浏览事件衡量用户与浏览结果的交互
- 推荐事件衡量用户与推荐的交互
- 转换事件衡量如
添加到购物车
或购买
等用户事件
自动完成事件
// Track when the user focuses into the search bar
constructorIO.trackInputFocus(searchTerm: "")
// Track when the user selects an autocomplete suggestion
constructorIO.trackAutocompleteSelect(searchTerm: "toothpicks", originalQuery: "tooth", sectionName: "Search Suggestions", group: CIOGroup(displayName: "Dental Health", groupID: "dental-92dk2", path: "health-2911e/dental-92dk2"), resultID: "179b8a0e-3799-4a31-be87-127b06871de2")
// Track when the user submits a search (either by selecting a suggestion or not selecting a suggestion)
constructorIO.trackSearchSubmit(searchTerm: "toothpicks", originalQuery: "tooth")
搜索事件
// Track when search results are loaded into view (customer ID's are the ID's of shown items)
constructorIO.trackSearchResultsLoaded(searchTerm: "tooth", resultCount: 789, customerIDs: ["1234567-AB", "1234765-CD", "1234576-DE"])
// Track when a search result is clicked
constructorIO.trackSearchResultClick(itemName: "Fashionable Toothpicks", customerID: "1234567-AB", variationID: "1234567-AB-7463", searchTerm: "tooth", sectionName: "Products", resultID: "179b8a0e-3799-4a31-be87-127b06871de2")
浏览事件
// Track when browse results are loaded into view
constructorIO.trackBrowseResultsLoaded(filterName: "Category", filterValue: "Snacks", resultCount: 674)
// Track when a browse result is clicked
constructorIO.trackBrowseResultClick(filterName: "Category", filterValue: "Snacks", customerID: "7654321-BA", variationID: "7654321-BA-738", resultPositionOnPage: 4, sectionName: "Products", resultID: "179b8a0e-3799-4a31-be87-127b06871de2")
推荐事件
// Track when recommendation results are viewed
constructorIO.trackRecommendationResultsView(podID: "pdp_best_sellers", numResultsViewed: 5, resultPage: 1, resultCount: 10, resultID: "179b8a0e-3799-4a31-be87-127b06871de2")
// Track when a recomendation result is clicked
constructorIO.trackRecommendationResultClick(podID: "pdp_best_sellers", strategyID: "best_sellers", customerID: "P183021", variationID: "7281930", numResultsPerPage: 30, resultPage: 1, resultCount: 15, resultPositionOnPage: 1, resultID: "179b8a0e-3799-4a31-be87-127b06871de2")
转换事件
// Track when an item converts (a.k.a. is added to cart) regardless of the user journey that led to adding to cart
constructorIO.trackConversion(itemName: "Fashionable Toothpicks", customerID: "1234567-AB", variationID: "1234567-AB-47398", revenue: 12.99, searchTerm: "tooth", conversionType: "add_to_cart")
// Track when items are purchased
constructorIO.trackPurchase(customerIDs: ["123-AB", "456-CD"], revenue: 34.49, orderID: "343-315")
// Tracking items w/ variations in purchases (supported in v2.5.5 and above)
let purchaseItems = [
CIOItem(customerID: "custID1", variationID: "varID1", quantity: 2),
CIOItem(customerID: "custID2", variationID: "varID2", quantity: 3)
]
constructorIO.trackPurchase(items: purchaseItems, revenue: 93.89, orderID: "423-2432")
其他事件
// Track when a product detail page is loaded (a.k.a after a user clicks on an item)
constructorIO.trackItemDetailLoad(customerID: "10001", itemName: "item1", variationID: "var1", sectionName: "Products")