BetterAnalyticsService - An AnalyticsService API Wrapper

Roblox recently introduced Economy and Funnel dashboards under Creator Analytics along with some new APIs for AnalyticsService. These events let developers keep track of how in-game currency is being used and monitor player behaviour (For example, 20% of players left the game at stage 7).

I decided to write a simple module that acts as a wrapper for AnalyticsService. It implements better error handling with Pcalls and comes with a debug mode in the config to examine the data being published.

You can get the model on the Creator Store or on Github.

Usage:
local ServerScriptService = game:GetService("ServerScriptService")
local BetterAnalyticsService = require(ServerScriptService.BetterAnalyticsService)

BetterAnalyticsService:LogEconomyEvent(
	player, -- Player Instance
	"Source", -- Flow Type: Can be "Source" or "Sink"
	"Cash", -- Currency
	100, -- Amount Of Currency
	player.leaderstats.Cash.Value, -- Current balance
	"Gameplay", -- Transaction Type: Can be "IAP", "Shop", "Gameplay", "ContextualPurchase", "TimedReward" or "Onboarding"
	"Winner" -- Item SKU: Optional
)

-- One-Time/Onboarding Funnel
BetterAnalyticsService:LogOnboardingFunnelStepEvent(
	player, --Player Instance
	1, -- Step Number
	"Picked Starter Character" -- Step Name
)

-- Recurring Funnel
BetterAnalyticsService:LogFunnelStepEvent(
	player, -- Player Instance
	"Starter Pack", -- Funnel Name
	nil, -- Funnel Session ID: Optional, Module uses HttpService:GenerateGUID() as fallback 
	1, --Step Number
	"Opened Shop" --Step Name
)

Find Out More: Event Types | Documentation - Roblox Creator Hub
AnalyticsService: AnalyticsService | Documentation - Roblox Creator Hub
Economy Flow Types: AnalyticsEconomyFlowType | Documentation - Roblox Creator Hub
Economy Transaction Types: AnalyticsEconomyTransactionType | Documentation - Roblox Creator Hub

I plan to update this with more functionality over time as these analytics events are fairly new. While this isn’t something revolutionary, here are a few reasons to use this rather than directly using the service:

  • Better error handling
  • The module handles enumeration
  • Lower API calls, AnalyticsService has Rate Limits

This module uses the GPLv3 license. You can copy, modify, convey, adapt, and/or redistribute this work.

Bug reports and constructive criticism is highly appreciated, Thank you.

9 Likes

There are two main things I noticed which I think you would like to know.

1) I had to change my economy flow type to a string. I think by default you should expect an enum item, as that’s what the Roblox one does (makes converting to your module convenient). And I think you should do the same for all enum related parameters. If you’re unsure on enum annotations you can do this:

local function expectString(flowType:string) end -- string
local function expectEnum(flowType:Enum.AnalyticsEconomyFlowType) end -- enum

2) I noticed you have restricted EconomyTransactionType to the ones Roblox provided:

local success, transactionTypeEnum = pcall(function()
	return Enum.AnalyticsEconomyTransactionType[transactionType]
end)

However, you can actually put whatever string you want in there. I’m not sure if it was intended for simplicity, but I’d like to use custom ones.

One extra thing which is specific to me:
I wanted to use this because I thought it would handle multiple calls in a way that prevents hitting the rate limit. For some cases I can see just using pcall would be good enough, but not exactly what I need.

Thanks for making the module though!

  1. This is something I could definitely look into
  2. You’re right, it is intended for simplicity but I’ll try to implement something that lets you pass custom values
    Thanks for your feedback, it is appreciated
1 Like