diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3ef1245e8..d97d59a98 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,6 +11,7 @@ class ApplicationController < ActionController::Base before_action :ensure_signup_complete before_action :set_locale + before_action :track_email_campaign check_authorization unless: :devise_controller? self.responder = ApplicationResponder @@ -107,4 +108,11 @@ class ApplicationController < ActionController::Base redirect_to(account_path, notice: t('verification.redirect_notices.already_verified')) end end + + def track_email_campaign + if params[:track_id] + campaign = Campaign.where(track_id: params[:track_id]).first + ahoy.track campaign.name if campaign.present? + end + end end diff --git a/app/models/campaign.rb b/app/models/campaign.rb new file mode 100644 index 000000000..3ce027734 --- /dev/null +++ b/app/models/campaign.rb @@ -0,0 +1,2 @@ +class Campaign < ActiveRecord::Base +end diff --git a/db/migrate/20151020135111_create_campaigns.rb b/db/migrate/20151020135111_create_campaigns.rb new file mode 100644 index 000000000..192b7d615 --- /dev/null +++ b/db/migrate/20151020135111_create_campaigns.rb @@ -0,0 +1,10 @@ +class CreateCampaigns < ActiveRecord::Migration + def change + create_table :campaigns do |t| + t.string :name + t.string :track_id + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 703ddd0d7..436fe1b7f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,7 +12,6 @@ # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 20151021113348) do - # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -48,6 +47,13 @@ ActiveRecord::Schema.define(version: 20151021113348) do add_index "ahoy_events", ["user_id"], name: "index_ahoy_events_on_user_id", using: :btree add_index "ahoy_events", ["visit_id"], name: "index_ahoy_events_on_visit_id", using: :btree + create_table "campaigns", force: :cascade do |t| + t.string "name" + t.string "track_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "comments", force: :cascade do |t| t.integer "commentable_id" t.string "commentable_type" diff --git a/lib/tasks/campaigns.rake b/lib/tasks/campaigns.rake new file mode 100644 index 000000000..d68cb7bd6 --- /dev/null +++ b/lib/tasks/campaigns.rake @@ -0,0 +1,8 @@ +namespace :campagins do + + desc "Recalculates all the comment counters for debates and proposals" + task create: :environment do + 3.times { |i| Campaign.create!(name: "Campaign#{i+1}", track_id: rand(2**32..2**64)) } + end + +end diff --git a/spec/factories.rb b/spec/factories.rb index a3784678e..eff656edf 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -268,4 +268,9 @@ FactoryGirl.define do started_at DateTime.now end + factory :campaign do + sequence(:name) { |n| "Campaign #{n}" } + sequence(:track_id) { |n| "#{n}" } + end + end diff --git a/spec/features/campaigns_spec.rb b/spec/features/campaigns_spec.rb new file mode 100644 index 000000000..525c292f6 --- /dev/null +++ b/spec/features/campaigns_spec.rb @@ -0,0 +1,33 @@ +require 'rails_helper' + +feature 'Email campaigns' do + + background do + @campaign1 = create(:campaign) + @campaign2 = create(:campaign) + + admin = create(:administrator) + login_as(admin.user) + end + + scenario "Track email templates" do + 3.times { visit root_url(track_id: @campaign1.track_id) } + 5.times { visit root_url(track_id: @campaign2.track_id) } + + visit admin_stats_path + + expect(page).to have_content "#{@campaign1.name} (3)" + expect(page).to have_content "#{@campaign2.name} (5)" + end + + scenario "Do not track erroneous track_ids" do + visit root_url(track_id: @campaign1.track_id) + visit root_url(track_id: "999") + + visit admin_stats_path + + expect(page).to have_content "#{@campaign1.name} (1)" + expect(page).to_not have_content "#{@campaign2.name}" + end + +end \ No newline at end of file