Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion app/controllers/api/schools_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

module Api
class SchoolsController < ApiController
MARKETING_PARAMETER_KEYS = %i[
utm_source
utm_medium
utm_campaign
utm_term
utm_content
gclid
fbclid
].freeze

before_action :authorize_user
load_and_authorize_resource
skip_load_and_authorize_resource only: :import
Expand All @@ -20,7 +30,13 @@ def create

if result.success?
@school = result[:school]
track_event('School - Created', school_id: @school.id)
track_event(
'School - Created',
marketing_parameters.merge(
school_id: @school.id,
first_landing_page: params[:first_landing_page]
)
Comment on lines +35 to +38
)
Comment thread
Copilot marked this conversation as resolved.
Comment thread
jamiebenstead marked this conversation as resolved.
Comment on lines +33 to +39
render :show, formats: [:json], status: :created
else
render json: {
Expand Down Expand Up @@ -77,6 +93,13 @@ def import

private

def marketing_parameters
marketing_params = params[:marketing_parameters]
return {} unless marketing_params.is_a?(ActionController::Parameters)

marketing_params.permit(*MARKETING_PARAMETER_KEYS).to_h
end

def create_params
params.expect(
school: %i[name
Expand Down
36 changes: 36 additions & 0 deletions spec/features/school/creating_a_school_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,40 @@
time: be_within(1.second).of(Time.current)
)
end

it 'records marketing parameters as top-level event properties' do
post(
'/api/schools',
headers:,
params: params.merge(
first_landing_page: '/signup',
marketing_parameters: {
utm_source: 'newsletter',
utm_campaign: 'back-to-school',
unexpected_email: 'person@example.com'
}
)
)

properties = Event.last.properties
Comment on lines +84 to +85
expect(properties).to include(
'school_id' => School.last.id,
'first_landing_page' => '/signup',
'utm_source' => 'newsletter',
'utm_campaign' => 'back-to-school'
)
expect(properties).not_to have_key('marketing_parameters')
expect(properties).not_to have_key('unexpected_email')
end

it 'ignores malformed marketing parameters' do
post(
'/api/schools',
headers:,
params: params.merge(marketing_parameters: 'newsletter')
)

expect(response).to have_http_status(:created)
expect(Event.last.properties).to eq('school_id' => School.last.id)
end
end
Loading