Class: ReservationTracking

Inherits:
ApplicationRecord show all
Defined in:
app/models/reservation_tracking.rb

Overview

ReservationTracking

Stores **tracking, attribution, and analytics fields** for a reservation. These fields are not part of the core business logic and are intentionally stored separately to avoid bloating the Reservation or ReservationProperty models.

Example attributes:

  • user_country

  • UTM parameters (utm_source, utm_medium, utm_campaign, utm_term, utm_content)

  • future marketing identifiers (gclid, fbclid, ad_id, referral_code)

Each Reservation has at most *one* tracking record.

TODO: migrate UTM fields from ReservationProperty to ReservationTracking.

Instance Method Summary collapse

Methods inherited from ApplicationRecord

sync_carrierwave_url

Instance Method Details

#user_countryString?

Returns the user's country name for analytics tracking. The value corresponds to the official ISO-3166 country name stored in the Country table.

ref: en.wikipedia.org/wiki/List_of_ISO_3166_country_codes

or nil if no country is associated.

Returns:

  • (String, nil)

    The full ISO-3166 country name (e.g., “Thailand”, “United States of America”),



78
79
80
# File 'app/models/reservation_tracking.rb', line 78

def user_country
  country&.name
end

#user_country=(country_alpha3) ⇒ Object

Sets the user_country for analytics tracking.

This method looks up the country by alpha3 code in the database. If found, it links the country; otherwise, country_id will be set to nil. No error is raised for non-existent countries, as this is used purely for tracking user origin.

NOTE: The validation of whether the country alpha3 code is in the database happens in the service layer via Country.valid_country_code? before this setter is called.

Examples:

tracking.user_country = "IDN"         # Sets country_id if Indonesia exists in DB, nil otherwise
tracking.user_country = "THA"         # Sets country_id to Thailand's id
tracking.user_country = "XYZ"         # Sets country_id to nil (non-existent country)
tracking.user_country = ""            # Sets country_id to nil
tracking.user_country = nil           # Sets country_id to nil

Parameters:

  • country_alpha3 (String, nil)

    The alpha3 code of the user's country (e.g., “IDN” for Indonesia, “VNM” for Vietnam, “THA” for Thailand)



68
69
70
# File 'app/models/reservation_tracking.rb', line 68

def user_country=(country_alpha3)
  self.country = Country.find_by(alpha3: country_alpha3) if country_alpha3.present?
end