Recommendation Service
概要
方法
GenerateItemListAsync(generateRecommendationItemListRequest: Dictionary):RecommendationPages |
GetRecommendationItemAsync(itemId: string):Dictionary |
LogActionEvent(actionType: Enum.RecommendationActionType,itemId: string,tracingId: string,actionEventDetails: Dictionary):() |
LogImpressionEvent(impressionType: Enum.RecommendationImpressionType,itemId: string,tracingId: string,impressionEventDetails: Dictionary):() |
LogPreferenceEvent(preferenceType: Enum.RecommendationPreferenceType,targetType: Enum.RecommendationPreferenceTargetType,targetId: string,tracingId: string,itemId: string):() |
RegisterItemAsync(player: Player,registerRecommendationItemsRequest: Dictionary):Dictionary |
RemoveItemAsync(itemId: string):() |
UpdateItemAsync(updateRecommendationItemRequest: Dictionary):() |
API 參考
方法
Generate Item List Async
Recommendation Service: Generate Item List Async(generate Recommendation Item List Request:Dictionary):RecommendationPages
參數
範例程式碼
生成推薦物品列表
local RecommendationService = game:GetService("RecommendationService")
-- 定義生成推薦列表的請求
local request: GenerateRecommendationItemListRequest = {
ConfigName = "MaximizeEngagement",
LocationId = "Lobby",
PageSize = 10
-- 注意:取消註解並設置 CustomContexts.UserId 用於伺服器腳本。
-- 本地腳本不需要設置。
-- CustomContexts = {
-- ["UserId"] = tostring(player.UserId),
-- }
}
-- 調用 GenerateItemListAsync 以獲取推薦頁面
local success, recommendationPages = pcall(function()
return RecommendationService:GenerateItemListAsync(request)
end)
if success then
print("生成的推薦頁面:", recommendationPages)
else
warn("生成推薦失敗:", recommendationPages)
endGet Recommendation Item Async
參數
範例程式碼
獲取單個推薦項目
local RecommendationService = game:GetService("RecommendationService")
local function getItem(itemId)
local success, item = pcall(function()
return RecommendationService:GetRecommendationItemAsync(itemId)
end)
if success and item then
print("成功獲取物品: " .. item.ItemId)
print(" 參考Id: " .. item.ReferenceId)
-- 當直接獲取單個物品時,TracingId 將為空
if item.Creator then
print(" 創建者Id: " .. tostring(item.Creator.CreatorId))
end
if item.CustomTags then
print(" 自定義標籤: ")
for _, customTag in ipairs(item.CustomTags) do
print(" " .. customTag)
end
end
else
warn("獲取物品失敗: " .. itemId .. ". 錯誤: " .. tostring(item))
end
end
-- 示例用法 (需要有效的 itemId)
getItem("some-item-id")Log Action Event
Recommendation Service: Log Action Event(
action Type:Enum.RecommendationActionType, item Id:string, tracing Id:string, action Event Details:Dictionary
):()
參數
| 預設值:nil |
返回
()
範例程式碼
記錄用戶行為
local RecommendationService = game:GetService("RecommendationService")
local function logLike(itemId, tracingId)
local detail: RecommendationActionEventDetails = {
ReactionType = "Like",
Weight = 1.0
}
RecommendationService:LogActionEvent(Enum.RecommendationActionType.AddReaction, itemId, tracingId, detail)
print("已記錄 'Like' 行動的項目: " .. itemId)
end
-- 示例用法 (需要從 GenerateItemListAsync 獲取有效的 itemId 和 tracingId)
logLike("item-id", "tracing-id")Log Impression Event
Recommendation Service: Log Impression Event(
impression Type:Enum.RecommendationImpressionType, item Id:string, tracing Id:string, impression Event Details:Dictionary
):()
參數
| 預設值:nil |
返回
()
範例程式碼
記錄使用者印象
local RecommendationService = game:GetService("RecommendationService")
local function logView(itemId, tracingId)
local detail: RecommendationImpressionEventDetails = {
Duration = 5, -- 用戶查看了 5 秒
ItemPosition = 1,
DepartureIntent = Enum.RecommendationDepartureIntent.Neutral
}
RecommendationService:LogImpressionEvent(Enum.RecommendationImpressionType.View, itemId, tracingId, detail)
print("已記錄 '查看' 印象的項目: " .. itemId)
end
-- 示例用法 (需要從 GenerateItemListAsync 獲取有效的 itemId 和 tracingId)
logView("some-item-id", "some-tracing-id")Log Preference Event
Recommendation Service: Log Preference Event(
preference Type:Enum.RecommendationPreferenceType, target Type:Enum.RecommendationPreferenceTargetType, target Id:string, tracing Id:string, item Id:string
):()
參數
返回
()
Register Item Async
Recommendation Service: Register Item Async(
參數
範例程式碼
註冊新項目
local RecommendationService = game:GetService("RecommendationService")
local storage = game:GetService("ReplicatedStorage")
-- 假設在 ReplicatedStorage 中存在一個名為 'RegisterItemRemote' 的 RemoteFunction
local registerItemRemote = storage.RegisterItemRemote
registerItemRemote.OnServerInvoke = function(player, refId)
local request: RegisterRecommendationItemRequest = {
ContentType = Enum.RecommendationItemContentType.Dynamic,
ReferenceId = refId,
Duration = 60,
Visibility = Enum.RecommendationItemVisibility.Public,
CustomTags = {"locale:en-us", "seasonal:summer"},
Attributes = {
{
AssetId = 123,
Description = "測試視頻",
TrimStartTime = 0,
TrimEndTime = 10.5
}
}
}
local success, response = pcall(function()
return RecommendationService:RegisterItemAsync(player, request)
end)
if success and response then
print("成功註冊項目。項目ID: " .. response.ItemId)
return response.ItemId
else
warn("註冊項目失敗。錯誤: " .. tostring(response))
return nil
end
endRemove Item Async
參數
返回
()
範例程式碼
移除項目
local RecommendationService = game:GetService("RecommendationService")
local function removeItem(itemId)
local success, err = pcall(function()
return RecommendationService:RemoveItemAsync(itemId)
end)
if success then
print("成功移除物品: " .. itemId)
else
warn("移除物品失敗: " .. itemId .. ". 錯誤: " .. tostring(err))
end
end
removeItem("itemId")Update Item Async
參數
返回
()
範例程式碼
更新項目
local RecommendationService = game:GetService("RecommendationService")
local function updateItem(itemId)
-- 只包含您想要更新的字段
local request: UpdateRecommendationItemRequest = {
ItemId = itemId,
Visibility = Enum.RecommendationItemVisibility.Private,
-- 注意:整個 CustomTags 列表將被這個新列表替換
CustomTags = {"updated-tag"}
}
local success, err = pcall(function()
return RecommendationService:UpdateItemAsync(request)
end)
if success then
print("成功更新項目: " .. itemId)
else
warn("更新項目失敗: " .. itemId .. ". 錯誤: " .. tostring(err))
end
end
-- 示例用法
updateItem("some-item-id-to-update")