Kevinleary.net, LLC.

TikTok Conversion & Attribution Tracking

Track conversions for TikTok ads with high match rate attribution using both the Pixel and Events API.

TikTok advertising is quickly becoming a major advertising channel for US brands. Facebook’s limitations on audiences and targeting approach for certain business categories (like financial) have made other advertising platforms like TikTok more beneficial dollar for dollar.

I’ve done a handful of TikTok pixel tracking implementations, and the most thorough and reliable approach involves a dual tracking strategy that uses the client-side TikTok Pixel and the server-side Events API. Events are tracked in the browser and on the server in duplicate, but they share the same ID property, which allows TikTok to automatically de-duplicate for you. This approach provides a rock solid approach that tracks events for all users, including users with Apple ITP enabled in the latest iOS, or an active ad blocker.

TikTok Attribution: How it works

Pixel Tracking Implementation

The first step begins with implementing the TikTok pixel. It can be installed simple by adding a script to your websites pages like any other tracking pixel, but the real advantage comes with advanced audience matching.

Installing The Pixel

To install the TikTok pixel:

  1. Login to your Ads Manager account at ads.tiktok.com
  2. Go to Assets > Events > Web Events section and click the Manage button
  3. Click the Set Up Web Events button
  4. Enter your website’s name as the Pixel Name, choose TikTok Pixel from the two option boxes, and then click Next
  5. For the most flexibility to implement an ideal strategy, choose Manually Install Pixel Code and then continue
  6. Choose Custom Code and hit Next to create the pixel

TikTok Pixel Install Steps

At this point, you’ll see the dialog close, and there will be a set of additional technical steps you’ll need to take to implement the pixel on your website.

  1. Copy the pixel code and install it on your site, either directly in the codebase on every page or through tag management system like Segment.com or Google Tag Manager
  2. Enable the Allow first-party cookie option
  3. Enable the Automatic advanced matching option and then check both Phone Number and Email options

The pixel code will look like this:

<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('16372837AHDJDTAYDJ181');
  ttq.page();
}(window, document, 'ttq');
</script>

Verify The Installation

Once these steps are complete, you’ll want to verify that the installation is correct and working properly. To do this, you can use the TikTok Pixel Helper browser extension. This extension will allow you to understand how and where the pixel is loading on your website. You’ll see details about advanced matching, and events fired, allowing you to confirm everything is working properly without errors.

Tracking Standard Events

TikTok supports the following standard events for tracking your funnels. They’re familiar and share similarities with Facebook’s Pixel, among other advertising events, but it’s worth mentioning how I specifically have seen companies using them in both B2C and B2B contexts.

  • ViewContent: When a page is viewed on a site, but I usually recommend this for pages directly involved in a lead generation funnel. There’s a built-in pageview event for standard pageviews.
  • ClickButton: Tracking button clicks throughout a site
  • Search: User search is entered
  • AddToWishlist: Usually not used, but I have considered it for tracking a social share or any type of bookmark or save for later feature
  • AddToCart: User becomes a prospect, usually when they click a button or take an action to begin a lead funnel process
  • InitiateCheckout: First value is collected during a lead funnel
  • AddPaymentInfo: Final payment details are collected during a lead funnel
  • CompletePayment: Payment information is verified as valid
  • PlaceAnOrder: Order or product purchase is made
  • Contact: Contact or consultation occurs.
  • Download: File downloads, in the context of a B2B site it would often be resources like PDF’s or other supporting files
  • SubmitForm: Form is submitted
  • CompleteRegistration: Registration is completed, if you have an online application that pre-qualifies but not fully qualifies a person this is a good event to use
  • Subscribe: Subscription is made to a newsletter

To track any of these events you’ll use the ttq.track method:

ttq.track('CompleteRegistration');

Some of the events allow recommended parameters, more information can be found in TikTok’s documentation.

Advanced Matching

In addition to tracking pixel and API based events, I’d recommend configuring automatic and manual advanced matching. Advanced matching for the web pixel allows you to send TikTok encrypted customer data that helps to identify them as a user. This approach is also officially recommended by TikTok as well:

We recommend advertisers use both Manual and Automatic Advanced Matching at the same time, if possible, as this setting maximizes the full performance of Advanced Matching.

Currently, advanced matching uses only email and phone numbers, but I expect this to change in the future. Other platforms like Google Enhanced Conversions and Facebook Advanced Matching utilize additional properties like ZIP code, city, state, address, first name, last name and more. It’s only a matter of time before TikTok see’s value in similar approaches.

Automatic & Manual Advanced Tracking with the TikTok Pixel

Currently, this feature isn’t well documented on the US TikTok tracking documentation site, but to implement manual advanced matching, the ttq.identify() method can be used to pass in a user’s phone number and email address when they’re collected.

ttq.identify({
  email: '[email protected]',
  phone_number: '+15551234567',
})

The phone number must be in international format like this, otherwise you’ll receive tracking diagnostic errors. More information can be found deep within the TikTok pixel documentation.

Manual Advanced Matching with Events API

Advanced matching with the events API is manual only. You’ll have to send in the specific values that will be used in the context.user object of an event payload. These values also need to be encrypted as a sha256 hash.

If you’re using PHP the following function can be used to encrypt user.email and user.phone values sent in with Events API calls:

/**
 * SHA256 Encryption
 */
function sha256( $data ) {
  return hash( 'sha256', $data );
}

Other web programming languages share a similar approach, just look for a method to generate a sha256 hash. Once you’ve encrypted the values, you’ll add them onto your event payload as the context.user object. This passes them to TikTok for use with manual advanced matching.

Server-side Events API Tracking

The TikTok Events API allows you to track customer activity on a website or web application using server-side code. This generally involves more technical overhead, but result in more reliable tracking. When properly configured, the conversion events you track will happen for every user that passes through your funnel. Ad blockers and intelligent tracking protection will not be blocked by server-side events by design.

A typical JSON payload to the Events API looks like this:

{
  "pixel_code": "{PIXEL-ID}",
  "event_id": "{UUID}",
  "timestamp": 1670353390,
  "event": "CompleteRegistration"
  "context": {
    "page": {
      "url": "https://apply.website.com/signup-complete"
    },
    "user": {
      "email": "{SHA256-EMAIL}",
      "phone_number": "{SHA256-PHONE}",
      "external_id": "{SHA256-USER-ID}"
    },
    "client_user_agent": "Mozilla/5.0 (Linux; Android 13; Pixel 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Mobile Safari/537.36",
    "ip": "{IP-ADDRESS}"
  },
  "properties": {
    "description": "New customer signup successfully completed",
    "content_type": "product",
    "content_id": "/signup-complete",
    "content_category": "apply.website.com"
  }
}

TikTok Tracking with Pixel + Events API

To implement the Events API alongside pixel tracking, you’ll need to create a single unique ID for each event, and then send that ID in to both the pixel and the Events API with each individual event that occurs.

As a real-world example, let’s say that you track a CompleteRegistration event on your website when a new user signup form or onboarding flow is completed. Each time this event occurs, you would generate unique ID and send that in as the event_id to both the pixel using ttq.track() and the Events API’s /open_api/vX/pixel/track/ endpoint. Both events have the same ID, and this is how TikTok identifies them as the same event, automatically de-duplicating for you.

Pixel Code

Tracking this CompleteRegistration event with the TikTok pixel in Google Tag Manager would look something like this:

ttq.identify({
  email: '{{Email}}'
  phone_number: '{{Phone}}',
})
ttq.track('CompleteRegistration', {
  event_id: '{{Event ID}}',
  content_type: 'product',
  content_id: 'account-signup',
  currency: 'USD'
});

Events API Code

Tracking this CompleteRegistration event in server-side code using the TikTok Events API would look something like this:

curl --location --request POST 'https://business-api.tiktok.com/open_api/v1.2/pixel/track/' \
--header 'Access-Token: {API-ACCESS-TOKEN}' \
--header 'Content-Type: application/json' \
--data-raw '{
  "pixel_code": "{PIXEL-ID}",
  "event_id": "{UUID}",
  "timestamp": 1670353390,
  "event": "CompleteRegistration"
  "context": {
    "page": {
      "url": "https://apply.website.com/signup-complete"
    },
    "user": {
      "email": "{SHA256-EMAIL}",
      "phone_number": "{SHA256-PHONE}",
      "external_id": "{SHA256-USER-ID}"
    },
    "client_user_agent": "Mozilla/5.0 (Linux; Android 13; Pixel 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Mobile Safari/537.36",
    "ip": "{IP-ADDRESS}"
  },
  "properties": {
    "description": "New customer signup successfully completed",
    "content_type": "product",
    "content_id": "/signup-complete",
    "content_category": "apply.website.com"
  }
}

Conclusion

This isn’t quite a step-by-step guide, but it should provide a solid foundation for how to set up an ideal TikTok event tracking implementing at your organization using both the TikTok Pixel and Events API. This approach will keep your advertising attribution solid in 2023, as mobile and web browsers continue to tighten privacy policies.

If you’re looking for help with your TikTok advertising attribution implementation or strategy, let me know, I’m available for hire and have a lot of experience setting up rock solid implementations that are backed by results.

Meet the Author

Kevin Leary, WordPress Consultant

I'm a freelance web developer and WordPress consultant in Boston, MA with 16 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.