Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
g3ortega committed Apr 24, 2024
1 parent e23505b commit 117ac76
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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:

Expand All @@ -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.
Expand All @@ -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

Expand Down
14 changes: 13 additions & 1 deletion lib/utm_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions lib/utm_conversion/capture/utm_params_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/utm_conversion/storage/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/utm_conversion/storage/in_memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 117ac76

Please sign in to comment.