setCustomVariable

❗️

Deprecated

This method is no longer recommended. Use the setCustomDimension() method instead.

The setCustomVariable() method sets a custom variable in the screen (action) or visit (session) scope. The value can be sent to Piwik PRO with a screen view or any other event.

Syntax

PiwikTracker.sharedInstance()?.setCustomVariable(
    index: index,
    name: "name",
    value: "value",
    scope: .action
)
[[PiwikTracker sharedInstance] setCustomVariableForIndex:index
                                                    name:@"name"
                                                   value:@"value"
                                                   scope:CustomVariableScopeAction];

Parameters

ParameterTypeRequiredDescription
indexnumberYesSlot for the variable. Must be ≥ 1. With default visit variables enabled, visit scope cannot use indexes 1–3 (see Notes).
namestringYesVariable name (UTF-8). The SDK does not truncate; keep names short to avoid oversized URLs.
valuestringYesVariable value (UTF-8). The SDK does not truncate; keep values short to avoid oversized URLs.
scope.visit or .actionYesSession-wide (visit) or only the next tracked interaction (action).

Examples

To set a custom variable in the screen (action) scope and send it with a screen view:

PiwikTracker.sharedInstance()?.setCustomVariable(
    index: 1,
    name: "Rating",
    value: "5",
    scope: .action
)
[[PiwikTracker sharedInstance]
    setCustomVariableForIndex:1
                         name:@"Rating"
                        value:@"5"
                        scope:CustomVariableScopeAction];

Notes

  • With includeDefaultCustomVariable = true (the default), visit-scoped variables must use an index greater than 3, because the SDK reserves indexes 1–3 for built-in visit variables (platform, OS, app version). This restriction does not apply to action scope; you can use any index ≥ 1 for screen-level variables.
  • Action scope: variables are sent with the next tracking call, then cleared. Visit scope: variables stay on the session until the session data is reset or replaced.
  • Deprecated API; prefer setCustomDimension() for new integrations.

Related methods