discourse/plugins/discourse-adplugin/app/controllers/ad_plugin/ad_impressions_controller.rb
Juan David Martínez Cubillos cc7f4743a5
FEATURE: Implement ad impressions tracking (#35797)
**Description**

This PR implements these features:
 - Impressions tracking for HouseAds and External Ads
 - Click tracking for HouseAds and External Ads
- Reports of HouseAds performance, ad impressions by user, general ad
impressions, conversion reports by ad and placement, conversion reports
by ad type, and conversion reports by placement.

As part of implementing these features, the HouseAd model had to be
pulled out of the Plugin rows table into its own table. Refactoring had
to be done to account for these changes.

---------

Co-authored-by: Bannon Tanner <bannon.n.tanner@gmail.com>
2025-12-01 12:17:25 -05:00

41 lines
1.1 KiB
Ruby
Vendored

# frozen_string_literal: true
module AdPlugin
class AdImpressionsController < ::ApplicationController
requires_plugin PLUGIN_NAME
skip_before_action :check_xhr, :preload_json, only: [:update]
def create
impression =
AdPlugin::AdImpression.create!(impression_params.merge(user_id: current_user&.id))
render json: impression.as_json(only: %i[id ad_type placement user_id])
end
def update
impression = AdPlugin::AdImpression.find(params[:id])
result = impression.record_click!
if result
render json: { success: true, clicked_at: impression.clicked_at }
else
render json: {
success: false,
error: I18n.t("errors.already_clicked"),
},
status: :unprocessable_entity
end
end
private
def impression_params
required_params =
params.require(:ad_plugin_impression).permit(:ad_type, :ad_plugin_house_ad_id, :placement)
required_params[:ad_type] = required_params[:ad_type].to_i
required_params
end
end
end