Skip to main content

Player Affiliates

Tracks affiliate/referral links with metadata. Records are initially created when an affiliate link is clicked (storing the affiliate code), then linked to a player after registration.

Table Comment

Tracks affiliate links with metadata. Initially stores just affiliate_code, then links to player_id after registration.

Relationships

  • Belongs to players via player_id (nullable — linked after registration)

Columns

ColumnTypeNullableDefaultDescription
iduuidNOgen_random_uuid()Primary key
player_iduuidYESFK to players. NULL until the visitor registers. Set after signup.
affiliate_codevarchar(255)NOThe hash parameter from the affiliate URL (e.g., d5daff1d82620648029999e65ae60f2b). Globally unique.
metadatajsonbNO'{}'JSONB field storing all URL parameters: promotion_id, device info, IP, country, UTM tags, referrer, etc.
created_attimestampNOCURRENT_TIMESTAMPWhen the affiliate link was first clicked/recorded
updated_attimestampNOCURRENT_TIMESTAMPLast update (managed by trigger)

Key Indexes

IndexColumnsNotes
player_affiliates_pkeyidPrimary key
player_affiliates_affiliate_code_keyaffiliate_codeUnique constraint
idx_player_affiliates_player_idplayer_idPlayer lookup
idx_player_affiliates_player_id_uniqueplayer_idUnique (WHERE player_id IS NOT NULL) — ensures one player per affiliate link
idx_player_affiliates_affiliate_codeaffiliate_codeFast code lookup
idx_player_affiliates_created_atcreated_atTime-based reporting

Triggers

TriggerFunctionDescription
trigger_update_player_affiliates_updated_atupdate_player_affiliates_updated_atAuto-updates updated_at on row change

Business Rules

  • The two-phase lifecycle:
    1. Link click: A record is created with affiliate_code and metadata but player_id = NULL
    2. Registration: The player_id is set when the visitor completes signup
  • The unique constraint on player_id (where not null) ensures each player can only be attributed to one affiliate source
  • The metadata JSONB captures the full marketing attribution data including UTM parameters, device info, and geo-data
  • Affiliate codes are used to attribute players to marketing campaigns and calculate affiliate commissions
  • Hidden coin packages with package_source = 'affiliate_hidden' can be unlocked via affiliate links