Class: Omnitrack::Adapters::TikTok

Inherits:
Base
  • Object
show all
Defined in:
lib/omnitrack/adapters/tiktok.rb

Overview

TikTok Events API (server-side). Docs: ads.tiktok.com/marketing_api/docs?id=1741601162187777

Required config:

pixel_id       TikTok Pixel ID
access_token   Events API access token

Constant Summary collapse

API_ENDPOINT =
"https://business-api.tiktok.com/open_api/v1.3/pixel/track/"
STANDARD_EVENTS =
%w[
  ViewContent ClickButton Search AddToWishlist AddToCart
  InitiateCheckout AddPaymentInfo PlaceAnOrder CompletePayment
  Download Register Subscribe Contact Submit
].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #logger

Instance Method Summary collapse

Methods inherited from Base

#enabled?, inherited, #initialize, #name

Constructor Details

This class inherits a constructor from Omnitrack::Adapters::Base

Instance Method Details

#identify_user(user_data = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/omnitrack/adapters/tiktok.rb', line 37

def identify_user(user_data = {})
  ctx = Omnitrack::Context.current
  ctx&.merge!(tiktok_user_data: normalize_user(user_data))

  Omnitrack::Result.success(
    adapter:  name,
    metadata: { note: "TikTok user data stored in context" }
  )
end

#track_conversion(data = {}) ⇒ Object



33
34
35
# File 'lib/omnitrack/adapters/tiktok.rb', line 33

def track_conversion(data = {})
  track_event("CompletePayment", data)
end

#track_event(event_name, payload = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/omnitrack/adapters/tiktok.rb', line 23

def track_event(event_name, payload = {})
  safe_execute(event_name) do
    body     = build_event_body(event_name, payload)
    response = http_post(API_ENDPOINT, body: body, headers: auth_headers)
    result   = JSON.parse(response.body)

    Omnitrack::Result.success(adapter: name, data: result)
  end
end