Tenancy and access
Where the Marketing tables live, what the tenant and website lifecycles delete, how retention runs, and why access has two levels.
The Marketing module isolates tenants the way the rest of the DMS does, by database instance, and keeps two global tables on purpose. This page explains that layout, the lifecycle rules built on it, and the two levels of access.
Tenancy is structural, like the rest of the DMS
These tables live in the DMS dms-tenant schema, one database instance per tenant:
- the analytics tables
marketing_events,marketing_sessions,marketing_website_statistics,marketing_page_snapshotsandmarketing_funnels marketing_funnel_trails, each session's funnel events, andmarketing_funnel_trail_backfills, how far each website's history is indexed into themmarketing_preferences, the website each member last selected in the consolemarketing_imports, the record of applied analytics imports
DMS core and dms-saas use the same model. Isolation is the instance boundary, not a tenantId filter that someone can forget.
Two tables are global, in the dms-core schema, on purpose:
marketing_websites. The public collect endpoint carries no JWT, so it needs awebsiteIdtotenantIdrouting step to land each beacon in the right instance. This table is the one surface still scoped by column (src/data-api/tenant-scope.ts).marketing_settings, a deployment singleton.
Everything else reads its tenant instance through GetModel(Model, tenantId) or @TenantScopedModel.
Tenant deletion and export
The module registers three hooks in construct(), as dms-saas does (src/hooks):
Hook.TENANT_DELETEDpurges the tenant's events, sessions, page snapshots, rollups, funnels, member preferences and applied imports, then its websites.Hook.USER_DELETEDforgets a deleted account's website selection in each of its workspaces.Hook.TENANT_DATA_EXPORTadds websites, funnels and rollups to the workspace export. Events, sessions and page snapshots stay out, because they are captures bounded by retention, not durable data that the workspace owns.
Website deletion and purge
DELETE /websites/:id purges the site's tracking data (events, sessions, rollup, page snapshots and applied imports) before it removes the row (services/tenant-website.ts). This matters beyond tidiness. The retention prune finds tenants through the websites table, so rows left behind by a tenant's last site would survive every retention pass. The site's funnel definitions and their A/B splits are deleted too, because without their website nobody could list or compute them.
POST /websites/:id/purge with scope: "all" runs the same purge but keeps the row and the funnels (purgeWebsiteCollectedData). With scope: "heatmaps" it deletes only clicks, scroll depths and page snapshots. A purge also drops the site's cached sessions, so a returning visitor opens a new one.
The dashboard's own site is never deleted, and a delete answers 409. Provisioning would only create it again.
Retention runs every hour
An hourly cron prunes rows past their retention (src/services/prune.ts, scheduled in src/index.ts). Raw events follow the raw-event retention. Sessions and rollup rows follow the longer statistics retention. Page snapshots follow the shorter of the snapshot retention and the raw-event retention. The cron also empties the top maps of rollup rows older than the readable window. Every instance runs the pass. Event timestamps never change, and rollup IDs encode a day that never changes, so a range delete on them stays valid after concurrent writes. Clearing old top maps changes no scalar counter.
Sessions and snapshots are different, because a row can become active again under the same ID. Their cleanup reads only the expired IDs and dates, then calls atomicMutation with deleteIfEqual on the observed lastSeenAt or capturedAt (src/db/models/retention.ts). The database adapter must compare the date and delete the scoped ID atomically. A predicate inside a generic range-delete statement does not give that guarantee on every backend. Date equality is enough for a fixed cutoff, so no revision counter or general ABA guarantee is needed. Only an applied result counts as a deletion. An unknown result stops that cleanup with an error, with no retry and no fallback to an unconditional delete.
The database interface and adapter must therefore implement deleteIfEqual. The module is validated with the published database interface, the MongoDB adapter that the package pins as a dev dependency, and the DMS release resolved at that time, without source overlays. pnpm test loads the real modules through AntelopeJS and runs MongoDB on a disposable database. The first run downloads MongoDB unless MONGOMS_SYSTEM_BINARY is set. Marketing integration tests on PostgreSQL and RethinkDB are still pending, so the MongoDB result is not a portability claim.
Access has two deliberate levels
Module pages (/modules/marketing/*) and the /api/marketing/tables/* DataControllers behind them are for the platform owner only (@AuthOwnerOnly). That follows the DMS convention that module pages are owner administration spaces.
The REST reads and writes (/api/marketing/blocks/*, /context, /stats/*, /websites and /funnels/*) are tenant-scoped, through @AuthTenantMember or @AuthTenantOwner. They answer for the caller's tenant claim. If a Marketing page ever moves to a category that tenants can reach, the API is already correct and only the page moves.
Settings stay owner-only at both levels, because the settings row is global to the deployment and drives prunes across every tenant.
Every block has a name in the roles tree
Every module block carries .meta() with a translated name, description and icon (page.marketing.blocks.<key> in the frontend catalogs). So do the stock blocks and containers that the pages declare (blockMeta() in src/pages/blocks.ts). A block's permission then reads as a name in the roles tree, never as a component name. Layout rows (GridRow) carry no meta.