|
|
# Remembers the campaign parameters a player arrived with, so the printed QR
|
|
|
# codes can be told apart in the admin dashboard.
|
|
|
#
|
|
|
# The QR codes point at the bare domain, which redirects to /:locale and drops
|
|
|
# the query string -- and the player row is not created until the language page
|
|
|
# posts to game#start. The tags are therefore parked in the session on the way
|
|
|
# in and read back out when the player is created.
|
|
|
#
|
|
|
# Matomo-style mtm_* parameters are accepted as aliases for the utm_* ones.
|
|
|
#
|
|
|
# Last touch wins: a second scan is a second visit, and the player row that
|
|
|
# follows it belongs to the code that was actually scanned.
|
|
|
module UtmTracking
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
SESSION_KEY = "utm".freeze
|
|
|
|
|
|
included do
|
|
|
before_action :capture_utm
|
|
|
end
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
def capture_utm
|
|
|
tags = Player::UTM_KEYS.index_with { |key|
|
|
|
clean_utm(params[key].presence || params[key.to_s.sub("utm_", "mtm_")])
|
|
|
}.compact
|
|
|
return if tags.empty?
|
|
|
|
|
|
# String keys: the session round-trips through the cookie, which has no
|
|
|
# symbols, so `utm_attributes` would stop matching after the redirect.
|
|
|
session[SESSION_KEY] = tags.transform_keys(&:to_s)
|
|
|
end
|
|
|
|
|
|
|
|
|
# Query parameters are typed by whoever holds the link, so they are trimmed
|
|
|
# to something a column and a dashboard row can live with.
|
|
|
def clean_utm(value)
|
|
|
value.to_s.strip.first(100).presence
|
|
|
end
|
|
|
|
|
|
|
|
|
def utm_attributes
|
|
|
(session[SESSION_KEY] || {}).slice(*Player::UTM_KEYS.map(&:to_s))
|
|
|
end
|
|
|
end
|