概要
方法
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 参考
方法
GenerateItemListAsync
RecommendationService:GenerateItemListAsync(generateRecommendationItemListRequest: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)GetRecommendationItemAsync
参数
代码示例
获取单个推荐项
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(" ReferenceId: " .. item.ReferenceId)
-- 获取单个项目时,追踪 ID 将为空
if item.Creator then
print(" CreatorId: " .. tostring(item.Creator.CreatorId))
end
if item.CustomTags then
print(" CustomTags: ")
for _, customTag in ipairs(item.CustomTags) do
print(" " .. customTag)
end
end
else
warn("检索项目失败: " .. itemId .. ". 错误: " .. tostring(item))
end
end
-- 示例用法(需要有效的 itemId)
-- getItem("some-item-id")LogActionEvent
RecommendationService:LogActionEvent(
actionType:Enum.RecommendationActionType, itemId:string, tracingId:string, actionEventDetails: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")LogImpressionEvent
RecommendationService:LogImpressionEvent(
impressionType:Enum.RecommendationImpressionType, itemId:string, tracingId:string, impressionEventDetails: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("已记录 'View' 印象,项目: " .. itemId)
end
-- 使用示例 (需要来自 GenerateItemListAsync 的有效 itemId 和 tracingId)
-- logView("some-item-id", "some-tracing-id")LogPreferenceEvent
RecommendationService:LogPreferenceEvent(
preferenceType:Enum.RecommendationPreferenceType, targetType:Enum.RecommendationPreferenceTargetType, targetId:string, tracingId:string, itemId:string
):()
参数
返回
()
RegisterItemAsync
RecommendationService:RegisterItemAsync(
参数
代码示例
注册新物品
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
endRemoveItemAsync
参数
返回
()
代码示例
移除项目
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
endUpdateItemAsync
参数
返回
()
代码示例
更新项目
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")