diff --git a/docs/providers/umami.md b/docs/providers/umami.md index 91bbf0e..ad9d6d6 100644 --- a/docs/providers/umami.md +++ b/docs/providers/umami.md @@ -10,6 +10,7 @@ umami: domain: your-domain.com websiteId: '' endpointUrl: https://umami.your-domain.com + distinctIdParameter: '' templates: tag: '' googleConsent: @@ -29,6 +30,7 @@ umami: | `domain` | Optional override for the event's domain field. | | `websiteId` | Umami website ID. | | `endpointUrl` | Base URL of your Umami instance. | +| `distinctIdParameter` | Optional event data key whose value the tag sends as the distinct ID. See [Distinct ID](#distinct-id). | | `googleConsent.mode` | Defaults to `analytics_storage`. | | `serverContainer.packages` | Events to forward. | | `templates.tag` | Path to a custom tag template file. Empty (default) provisions the embedded template. | @@ -43,3 +45,11 @@ Events without `Params` fields send no event data. The tag sends the first address of the `ip_override` event data value as `payload.ip`. This requires Umami v2.10.0 or later. If `payload.ip` is set, Umami v2.17.0 and later resolve the location from the IP and ignore the `cf-ip*` location headers. + +## Distinct ID + +With `distinctIdParameter` set, the tag reads that key from the server container event data and sends its value as `payload.id`. +The tag omits `payload.id` when the setting is empty or the value is missing. +Umami v3.4.0 and later add the distinct ID to the session hash, so visitors with the same IP and user agent get separate sessions. +The distinct ID does not join sessions across IP addresses, user agents or salt rotations. +Send the same value on every event of a visitor. An event without it starts a separate session. diff --git a/pkg/config/umami.go b/pkg/config/umami.go index 63d56ce..02c2c35 100644 --- a/pkg/config/umami.go +++ b/pkg/config/umami.go @@ -10,6 +10,9 @@ type Umami struct { Domain string `json:"domain" yaml:"domain"` WebsiteID string `json:"websiteId" yaml:"websiteId"` EndpointURL string `json:"endpointUrl" yaml:"endpointUrl"` + // Event data key whose value the tag sends as the Umami distinct ID (payload.id), e.g. a first-party session ID. + // Umami v3.4.0 and later separate sessions by it. Empty (default) sends no distinct ID. + DistinctIDParameter string `json:"distinctIdParameter,omitempty" yaml:"distinctIdParameter,omitempty"` // Custom tag template settings Templates UmamiTemplates `json:"templates" yaml:"templates"` // Google Consent settings diff --git a/pkg/provider/umami/server/tag/umami.go b/pkg/provider/umami/server/tag/umami.go index af81bd8..37bc09d 100644 --- a/pkg/provider/umami/server/tag/umami.go +++ b/pkg/provider/umami/server/tag/umami.go @@ -39,6 +39,14 @@ func NewUmami(name string, cfg config.Umami, template *tagmanager.CustomTemplate }, } + if cfg.DistinctIDParameter != "" { + parameter = append(parameter, &tagmanager.Parameter{ + Key: "distinctIdParameter", + Type: "template", + Value: cfg.DistinctIDParameter, + }) + } + if len(params) > 0 { var list []*tagmanager.Parameter diff --git a/pkg/provider/umami/server/tag/umami_test.go b/pkg/provider/umami/server/tag/umami_test.go index 8544a1f..5906ecf 100644 --- a/pkg/provider/umami/server/tag/umami_test.go +++ b/pkg/provider/umami/server/tag/umami_test.go @@ -73,3 +73,36 @@ func TestNewUmami(t *testing.T) { }) } } + +func TestNewUmami_DistinctIDParameter(t *testing.T) { + t.Parallel() + + find := func(tag *tagmanager.Tag, key string) *tagmanager.Parameter { + for _, p := range tag.Parameter { + if p.Key == key { + return p + } + } + + return nil + } + + t.Run("set", func(t *testing.T) { + t.Parallel() + + got := tag.NewUmami("page_view", config.Umami{DistinctIDParameter: "session_id"}, &tagmanager.CustomTemplate{}, nil) + + p := find(got, "distinctIdParameter") + require.NotNil(t, p) + assert.Equal(t, "template", p.Type) + assert.Equal(t, "session_id", p.Value) + }) + + t.Run("empty", func(t *testing.T) { + t.Parallel() + + got := tag.NewUmami("page_view", config.Umami{}, &tagmanager.CustomTemplate{}, nil) + + assert.Nil(t, find(got, "distinctIdParameter")) + }) +} diff --git a/pkg/provider/umami/server/template/umamidata.go b/pkg/provider/umami/server/template/umamidata.go index eea3f67..ea5fc3b 100644 --- a/pkg/provider/umami/server/template/umamidata.go +++ b/pkg/provider/umami/server/template/umamidata.go @@ -75,6 +75,13 @@ ___TEMPLATE_PARAMETERS___ } ], "defaultValue": 1000 + }, + { + "type": "TEXT", + "name": "distinctIdParameter", + "displayName": "Distinct ID Parameter (Optional)", + "simpleValueType": true, + "help": "Event data key whose value is sent as the Umami distinct ID (payload.id)" } ] }, @@ -144,6 +151,13 @@ if (pageLocation) { umamiEvent.payload.ip = ip; } + if (data.distinctIdParameter) { + const distinctId = eventData[data.distinctIdParameter]; + if (distinctId) { + umamiEvent.payload.id = makeString(distinctId); + } + } + const umamiData = getEventParameters(eventData, data.eventParameters); if (umamiData) { umamiEvent.payload.data = umamiData; diff --git a/sesamy.schema.json b/sesamy.schema.json index 4ad71d7..d8deef1 100644 --- a/sesamy.schema.json +++ b/sesamy.schema.json @@ -1071,6 +1071,10 @@ "endpointUrl": { "type": "string" }, + "distinctIdParameter": { + "type": "string", + "description": "Event data key whose value the tag sends as the Umami distinct ID (payload.id), e.g. a first-party session ID.\nUmami v3.4.0 and later separate sessions by it. Empty (default) sends no distinct ID." + }, "templates": { "$ref": "#/$defs/github.com.foomo.sesamy-cli.pkg.config.UmamiTemplates", "description": "Custom tag template settings" diff --git a/sesamy.yaml b/sesamy.yaml index fa093e4..75601e4 100644 --- a/sesamy.yaml +++ b/sesamy.yaml @@ -238,6 +238,8 @@ umami: websiteId: '' # Endpoint url of the umami api endpointUrl: https://umami.your-domain.com + # Optional event data key whose value is sent as the Umami distinct ID (payload.id) + distinctIdParameter: '' # Google Consent settings googleConsent: # Enable consent mode