Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tiktok Pixel #163

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/rack/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
require "rack/tracker/hubspot/hubspot"
require "rack/tracker/drift/drift"
require "rack/tracker/heap/heap"
require "rack/tracker/tiktok_pixel/tiktok_pixel"

module Rack
class Tracker
Expand Down
8 changes: 8 additions & 0 deletions lib/rack/tracker/tiktok_pixel/template/tiktok_pixel.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
!function (w, d, t) {
w.TiktokAnalyticsObject=t;var ttq=w[t]=w[t]||[];ttq.methods=["page","track","identify","instances","debug","on","off","once","ready","alias","group","enableCookie","disableCookie"],ttq.setAndDefer=function(t,e){t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}};for(var i=0;i<ttq.methods.length;i++)ttq.setAndDefer(ttq,ttq.methods[i]);ttq.instance=function(t){for(var e=ttq._i[t]||[],n=0;n<ttq.methods.length;n++)ttq.setAndDefer(e,ttq.methods[n]);return e},ttq.load=function(e,n){var i="https://analytics.tiktok.com/i18n/pixel/events.js";ttq._i=ttq._i||{},ttq._i[e]=[],ttq._i[e]._u=i,ttq._t=ttq._t||{},ttq._t[e]=+new Date,ttq._o=ttq._o||{},ttq._o[e]=n||{};var o=document.createElement("script");o.type="text/javascript",o.async=!0,o.src=i+"?sdkid="+e+"&lib="+t;var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(o,a)};

ttq.load('<%= tracker_options[:id] %>');
ttq.page();
}(window, document, 'ttq');
</script>
5 changes: 5 additions & 0 deletions lib/rack/tracker/tiktok_pixel/tiktok_pixel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Rack::Tracker::TiktokPixel < Rack::Tracker::Handler
self.position = :body
self.allowed_tracker_options = [:id]

end
26 changes: 26 additions & 0 deletions spec/handler/tiktok_pixel_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
RSpec.describe Rack::Tracker::TiktokPixel do
def env
{ 'PIXEL_ID' => 'DYNAMIC_PIXEL_ID' }
end

it 'will be placed in the body' do
expect(described_class.position).to eq(:body)
expect(described_class.new(env).position).to eq(:body)
end

describe 'with static id' do
subject { described_class.new(env, id: 'PIXEL_ID').render }

it 'will push the tracking events to the queue' do
expect(subject).to match(%r{ttq\.load\('PIXEL_ID'\)})
end
end

describe 'with dynamic id' do
subject { described_class.new(env, id: lambda { |env| env['PIXEL_ID'] }).render }

it 'will push the tracking events to the queue' do
expect(subject).to match(%r{ttq\.load\('DYNAMIC_PIXEL_ID'\)})
end
end
end
16 changes: 16 additions & 0 deletions spec/integration/tiktok_pixel_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'support/capybara_app_helper'

RSpec.describe "Tiktok Pixel Integration" do
before do
setup_app(action: :tiktok_pixel) do |tracker|
tracker.handler :tiktok_pixel, { id: 'PIXEL_ID' }
end
visit '/'
end

subject { page }

it "embeds the script tag from the controller action" do
expect(page).to have_content("ttq.load('PIXEL_ID');")
end
end
4 changes: 4 additions & 0 deletions spec/support/metal_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,8 @@ def drift
def heap
render "metal/index"
end

def tiktok_pixel
render "metal/index"
end
end