diff --git a/README.md b/README.md index d7549b2..92775bf 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ And then execute: 1. Configuration (Optional): -* Configure the storage adapter within a Rails initializer (e.g., config/initializers/utm_conversion.rb) using a block: +* Configure the storage adapter within a Rails initializer (e.g., `config/initializers/utm_conversion.rb`) using a block: ```ruby UTMConversion.configure do |config| @@ -34,6 +34,12 @@ end * The gem automatically includes the `UTMConversion::Capture::UTMParamsMiddleware` to capture UTM parameters from incoming requests and store them in the session and the configured storage adapter. +To include the middleware in your rails app you can add the following line to your `config/application.rb` file: + +```ruby +config.middleware.use UTMConversion::Capture::UTMParamsMiddleware +``` + 3. Accessing UTM Data: * In your controllers or models, you can access the captured UTM data using the `UTMConversion::Session::UTMData` class: @@ -44,6 +50,12 @@ if utm_data end ``` +If the data was already stored in the storage adapter (e.g., from a previous session), you can retrieve it using the `UTMConversion` module directly: + +```ruby +UTMConversion.retrieve_utm_data([session_id]) +``` + 4. Recording Conversions: * Implement the `record_conversion(session_id, event_data)` method in your chosen storage adapter to record conversions along with the associated session ID and any relevant event data. @@ -52,7 +64,8 @@ end * Storage Adapters: * Create custom storage adapters by extending the `UTMConversion::Storage::Base` class and implementing the required methods (`store`, `retrieve`, `record_conversion`). - * Refer to the `UTMConversion::Storage::DynamoDB` adapter for an example implementation. + + * Refer to the `UTMConversion::Storage::InMemory` adapter for an example implementation. ## Contributing diff --git a/lib/utm_conversion.rb b/lib/utm_conversion.rb index ab86fbc..d465fa2 100644 --- a/lib/utm_conversion.rb +++ b/lib/utm_conversion.rb @@ -18,7 +18,19 @@ class << self def configure yield self end + + def retrieve_utm_data(session_id) + storage_adapter.retrieve(session_id) + end + + def store_utm_data(session_id, utm_data) + storage_adapter.store(session_id, utm_data) + end + + def record_conversion(session_id, event_data) + storage_adapter.record_conversion(session_id, event_data) + end end - self.storage_adapter = UTMConversion::Storage::InMemory.new + self.storage_adapter ||= UTMConversion::Storage::InMemory.new end diff --git a/lib/utm_conversion/capture/utm_params_middleware.rb b/lib/utm_conversion/capture/utm_params_middleware.rb index 34bb5e7..e2843b2 100644 --- a/lib/utm_conversion/capture/utm_params_middleware.rb +++ b/lib/utm_conversion/capture/utm_params_middleware.rb @@ -25,9 +25,11 @@ def extract_utm_params(request) end end - def store_utm_data(utm_params, session) + def store_utm_data(session, utm_params) + return if utm_params.nil || utm_params == {} + UTMConversion::Session::UTMData.store(session, utm_params) - UTMConversion.storage_adapter.store(utm_params, session.id) if utm_params.present? + UTMConversion.storage_adapter.store(session.id, utm_params) end end end diff --git a/lib/utm_conversion/storage/base.rb b/lib/utm_conversion/storage/base.rb index e505492..d014e67 100644 --- a/lib/utm_conversion/storage/base.rb +++ b/lib/utm_conversion/storage/base.rb @@ -4,7 +4,7 @@ module UTMConversion module Storage # Base class for storage adapters class Base - def store(utm_params, session_id) + def store(session_id, utm_params) raise NotImplementedError end diff --git a/lib/utm_conversion/storage/in_memory.rb b/lib/utm_conversion/storage/in_memory.rb index ff0a411..0bbeab4 100644 --- a/lib/utm_conversion/storage/in_memory.rb +++ b/lib/utm_conversion/storage/in_memory.rb @@ -9,7 +9,7 @@ def initialize @data = {} end - def store(utm_params, session_id) + def store(session_id, utm_params) @data[session_id] = { utm_data: utm_params, conversions: [] } end