# Track purchases

> Record developer product and game pass purchases, wrap ProcessReceipt safely and give products friendly names.

Purchase tracking powers [Monetisation](https://docs.bloxlytics.app/analytics/monetisation), revenue on the overview, ARPU, paying players and purchase funnels. Most of it is automatic. The one thing you add is a wrapper around your `ProcessReceipt` handler, so completed developer product purchases are recorded from Roblox's authoritative receipt.

| What | How it is tracked | Event |
| --- | --- | --- |
| Game pass prompt closed | Automatically | `$purchase`, `status = "attempted"` |
| Game pass bought through a prompt | Automatically | `$purchase`, `status = "completed"` |
| Developer product prompt closed | Automatically | `$purchase`, `status = "attempted"` |
| Developer product granted | `Analytics:WrapProcessReceipt` | `$purchase`, `status = "completed"` |

All purchase events need the player to be in the server, and all of them stop when **Purchases** is turned off in the dashboard's SDK configuration.

## ProcessReceipt

```luau
Analytics:WrapProcessReceipt(handler: (receipt) -> Enum.ProductPurchaseDecision): (receipt) -> Enum.ProductPurchaseDecision
```

`WrapProcessReceipt` takes your existing handler and returns a new function to assign to `MarketplaceService.ProcessReceipt`. Your handler stays completely in charge of granting:

1. The wrapper calls your handler with the receipt, exactly as Roblox would.
2. If your handler returns `Enum.ProductPurchaseDecision.PurchaseGranted`, the wrapper records a completed purchase. Any other decision records nothing.
3. The wrapper returns your handler's decision unchanged.

Recording happens inside `pcall`, so a tracking problem can never change the decision or stop the receipt being processed. Errors raised by **your** handler are not caught: they behave exactly as they would without the wrapper.

```luau ServerScriptService/Receipts.server.luau
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local Analytics = require(game.ServerScriptService.Analytics)

local GEMS_BY_PRODUCT: { [number]: number } = {
	[1001] = 500,
	[1002] = 2500,
}

local function processReceipt(receipt): Enum.ProductPurchaseDecision
	local player = Players:GetPlayerByUserId(receipt.PlayerId)
	if not player then
		-- Roblox will try again the next time the player joins.
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	local gems = GEMS_BY_PRODUCT[receipt.ProductId]
	if not gems then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	-- Grant the product (and save it) before returning PurchaseGranted.
	local newBalance = grantGems(player, gems)
	Analytics:TrackCurrencyEarned(player, "Gems", gems, "Purchases", tostring(receipt.ProductId), newBalance)

	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketplaceService.ProcessReceipt = Analytics:WrapProcessReceipt(processReceipt)
```

`grantGems` stands for your own code that adds the currency and saves it.

The completed event records:

| Property | Value |
| --- | --- |
| `kind` | `"developer_product"` |
| `productId` | `receipt.ProductId` |
| `productName` | The product's name (see [product names](#product-names)). |
| `robux` | `receipt.CurrencySpent`, the Robux the player actually paid. |
| `status` | `"completed"` |
| `receiptId` | `receipt.PurchaseId`, Roblox's id for this purchase. |

### Things to know

- **Only granted purchases count.** If you return `NotProcessedYet`, nothing is recorded; when Roblox retries the receipt and you grant it, the purchase is recorded then.
- **The player must be in the server.** If `Players:GetPlayerByUserId(receipt.PlayerId)` finds no player, the purchase isn't recorded, even if you grant it.
- **Make your handler idempotent.** Roblox can deliver the same receipt more than once. If your handler returns `PurchaseGranted` for the same `PurchaseId` twice, two completions are recorded. Roblox recommends recording processed `PurchaseId`s anyway so you never grant twice.
- **It may yield once per product.** The first time a product is seen, the wrapper looks up its name with `MarketplaceService:GetProductInfo` before returning your decision. Later receipts for that product use the cache. Call [`SetProductNames`](#product-names) at start-up to avoid the lookup entirely.
- **`WrapProcessReceipt` checks its argument.** Passing something other than a function raises `WrapProcessReceipt expects your ProcessReceipt function`. This is the one SDK call that can error, because it runs once at start-up and a silent failure would leave you with no receipt handler.
- Roblox allows only one `ProcessReceipt` callback per server, so assign the wrapped function in one place.

## Game passes

With `AutoTrackGamePasses` left at its default (`true`), the SDK listens to `MarketplaceService.PromptGamePassPurchaseFinished`. Every time a game pass prompt closes it records an attempt, and if the player bought the pass, a completion with `receiptId` set to `gp-<userId>-<gamePassId>`. `robux` is the pass's `PriceInRobux` from `GetProductInfo`, so it is the current price when the lookup happens.

You don't need any code for this, as long as you prompt with `MarketplaceService:PromptGamePassPurchase`. Game passes bought outside your game (for example on the experience's store page) don't fire the prompt event, so they aren't recorded.

To grant the pass's benefits, keep your own `PromptGamePassPurchaseFinished` handler or `UserOwnsGamePassAsync` checks; the SDK only observes.

## Developer product attempts

When `MarketplaceService.PromptProductPurchaseFinished` fires, the SDK records an attempt for that player, whether or not they bought it. The completion comes only from `WrapProcessReceipt`, because the prompt result isn't proof the product was granted. Together they give you the `$purchase:attempted` → `$purchase:completed` steps used by the **First purchase** and **Shop conversion** funnel templates.

## Product names

Names and prices come from `MarketplaceService:GetProductInfo`, with `Enum.InfoType.Product` for developer products and `Enum.InfoType.GamePass` for game passes. Each product is looked up once per server and cached, including failed lookups. Prompt lookups run on their own thread, so they never slow your game down.

If you'd rather choose the names (or avoid the lookups), give them to the SDK:

```luau
Analytics:SetProductNames({
	[1001] = "500 Gems",
	[1002] = "2,500 Gems",
	[1003] = "Starter Pack",
})
```

- `SetProductNames` applies to **developer products only**. Game pass names always come from `GetProductInfo`.
- Call it at start-up, before any purchase. A product already looked up and cached on this server keeps its cached name.
- A product named this way is never looked up, so its prompt **attempts** carry no `robux`. Completions still carry `receipt.CurrencySpent`.
- You can call it more than once; later names add to or replace earlier ones.

Products seen in completed purchases are added to your project's product list in Bloxlytics, using these names.

## What isn't tracked

- **Failed purchases.** The API accepts `status = "failed"`, but the SDK never sends it: Roblox doesn't tell the server why a prompt closed without a purchase, so a closed prompt is recorded as an attempt.
- **Premium payouts.** The API accepts `kind = "premium"`, but the SDK doesn't send it; Roblox doesn't report Premium engagement payouts to game servers. Roblox's own revenue figures are available through [Open Cloud](https://docs.bloxlytics.app/projects/open-cloud); see [Roblox APIs Bloxlytics uses](https://docs.bloxlytics.app/roblox-apis#what-is-not-supported-and-why).
- **Purchases you track from the client.** There is no client API, on purpose.
