Skip to content
This repository has been archived by the owner on Aug 13, 2020. It is now read-only.

Commit

Permalink
Update conversion pixel to post only (#1330)
Browse files Browse the repository at this point in the history
* Update conversion pixel to post only
* Update instructions
  • Loading branch information
hopsoft authored Jun 12, 2020
1 parent 410b23d commit 4f85272
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 36 deletions.
4 changes: 3 additions & 1 deletion app/controllers/pixel_conversions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ def create
private

def pixel_conversion_params
params.permit(:pixel_id, :impression_id, :test, metadata: {}).tap do |whitelisted|
params.permit(:pixel_id, :impression_id, :test, :metadata).tap do |whitelisted|
whitelisted[:conversion_referrer] = request.referrer
whitelisted[:test] = whitelisted[:test].to_s.downcase == "true"
whitelisted[:metadata] = JSON.parse(whitelisted[:metadata]) if whitelisted[:metadata].present?
end
end
end
2 changes: 1 addition & 1 deletion app/javascript/packs/code_fund_conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import ConversionTracker from '../src/conversion-tracker'
window.CodeFund = new ConversionTracker(window.CodeFundConfig || {})

// Example
// CodeFund.recordConversion('12345')
// CodeFund.recordConversion('12345', { test: false, metadata: { ... } })
7 changes: 4 additions & 3 deletions app/javascript/src/conversion-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ export default class {
// TODO: update to use POST exclusively and support metadata
recordConversion (pixelId, options = { test: false, metadata: {} }) {
const { test, metadata } = options
const url = `${this.baseUrl}/pixels/${pixelId}/impressions/${this.impressionId}?test=${test}`
const url = `${this.baseUrl}/pixel/${pixelId}/impression/${this.impressionId}/conversions`
const params = `test=${!!test}&metadata=${JSON.stringify(metadata)}`
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (!this.successStatuses.includes(xhr.status))
console.log('CodeFund failed to record the conversion!', xhr.status)
}
}
xhr.open('GET', url)
xhr.send()
xhr.open('POST', url)
xhr.send(params)
}

// Indicates if the passed date (represented as a string from localStorage) has expired based on daysToLive
Expand Down
10 changes: 2 additions & 8 deletions app/models/pixel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,8 @@ class << self
# Records an conversion for the passed impression_id
# This operation is a noop if no impression is found
def record_conversion(impression_id_param, conversion_referrer: nil, test: false, metadata: {})
impression = begin
i = Impression.find_by(id: impression_id_param, organization_id: organization_id)
logger.info "Pixel#record_conversion Unable to find an impression for impression_id='#{impression_id_param}' and organization_id='#{organization_id}'." unless i
i
rescue => e
logger.info "Pixel#record_conversion Unable to find an impression for impression_id='#{impression_id_param}' and organization_id='#{organization_id}'. #{e.message}"
nil
end
impression = Impression.find_by(id: impression_id_param, organization_id: organization_id) if UUID.validate(impression_id_param)
logger.info "Pixel#record_conversion Unable to find an impression for impression_id='#{impression_id_param}' and organization_id='#{organization_id}'." unless impression
impression_attribute_names = %w[
advertiser_id
campaign_id
Expand Down
25 changes: 19 additions & 6 deletions app/views/pixels/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@
<%= render CardComponent.new do %>
<%= render(Card::HeaderComponent.new) { "Pixel Instructions" } %>
<%= render(Card::BodyComponent.new) do %>
<h6>Setup</h6>
<p>Place the following code snippet on all pages that your campaigns drive traffic to and all pages that track conversions.</p>
<h5>Setup</h5>

<pre class="border rounded p-3"><code class="code">&lt;script src="https://app.codefund.io/packs/js/conversion.js"&gt;&lt;/script&gt;</code></pre>
<div>Place the following code snippet on all pages that your campaigns drive traffic to and all pages that track conversions.</div>
<pre class="border rounded p-3 mt-2"><code class="code">&lt;script src="https://app.codefund.io/packs/js/conversion.js"&gt;&lt;/script&gt;</code></pre>

<h6>Pixel</h6>
<p>Place the following code snippet on the page that triggers the <strong><%= @pixel.name %></strong> conversion.</p>
<h5>Pixel</h5>

<pre class="border rounded p-3"><code class="code">&lt;script&gt;CodeFund.recordConversion('<%= @pixel.id %>')&lt;/script&gt;</code></pre>
<div>Place the following code snippet on the page that triggers the <strong><%= @pixel.name %></strong> conversion.</div>
<pre class="border rounded p-3 mt-2"><code class="code">&lt;script&gt;CodeFund.recordConversion('<%= @pixel.id %>')&lt;/script&gt;</code></pre>

<h6>Options</h6>
<ul>
<li>
<code>test</code> <em>- boolean indicating if this is a test</em>
<pre class="border rounded p-3 mt-2"><code class="code">&lt;script&gt;CodeFund.recordConversion('<%= @pixel.id %>', { test: true })&lt;/script&gt;</code></pre>
</li>

<li>
<code>metadata</code> <em>- object for custom metadata</em>
<pre class="border rounded p-3 mt-2"><code class="code">&lt;script&gt;CodeFund.recordConversion('<%= @pixel.id %>', { metadata: {custom: 'A Custom Value'} })&lt;/script&gt;</code></pre>
</li>
</ul>
<% end %>
<% end %>
<% end %>
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

# resource :global_stats, only: [:show], defaults: {format: :json}

match "/pixels/:pixel_id/impressions/:impression_id", to: "pixel_conversions#create", via: [:get, :post], as: "pixel_conversions"
scope "/pixel/:pixel_id/impression/:impression_id" do
resource :pixel_conversions, only: [:create], path: "conversions"
end

resources :jobs, only: [:index]
resources :job_posting_prospects, except: [:index, :destroy], path: "/jobs/listings"
Expand Down
17 changes: 1 addition & 16 deletions test/controllers/pixel_conversions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,11 @@ class PixelConversionsControllerTest < ActionDispatch::IntegrationTest
@impression.update(organization: @pixel.organization)
end

test "should create pixel conversion on GET" do
perform_enqueued_jobs do
get pixel_conversions_path(@pixel, @impression), params: {
test: true,
metadata: {"foo" => "bar"}
}
end
assert status == 202
assert_performed_jobs 1
conversion = @pixel.pixel_conversions.find_by(pixel: @pixel, impression: @impression)
assert conversion
assert conversion.test?
assert conversion.metadata["foo"] = "bar"
end

test "should create pixel conversion on POST" do
perform_enqueued_jobs do
post pixel_conversions_path(@pixel, @impression), params: {
test: true,
metadata: {"foo" => "bar"}
metadata: {"foo" => "bar"}.to_json
}
end
assert status == 202
Expand Down

0 comments on commit 4f85272

Please sign in to comment.