Server Side Analytics: TikTok
Requirements
The requirements here a basically to have and business TikTok account from which you need to obtain the proper keys required for the authentication.
Implementation
No official package here that can be used here so we need to make a custom logic how the events are sent.
Request parameters
event_source— depends from when the event is sent in this casewebevent_source_id— the pixel ID from TikTokdata— array containing the event data
[
'event' => $event_name,
'event_time' => now()->getTimestamp(),
'user' => [
'ttclid' => $click_id,
'email' => hash('sha256', strtolower(trim($user->email))),
'external_id' => hash('sha256', (string) $user->id),
],
'properties' => [
'contents' => [
[
'content_name' => $plan,
'price' => $amount_paid,
'quantity' => 1,
],
],
'currency' => 'USD',
'content_type' => 'product',
],
'page' => [
'url' => config('app.url'),
],
],Not all parameters are required, it’s up to you it’s what will be more helpful as information.
ttclid — the TikTok click ID can be obtained from the URL of the ad when it’s clicked it is required to be present in the payload as are event_name , event_id and the hashed user email.
Authorization
For this one the authorization is pretty straight forward you need an access token that can be obtained from the TikTok account dashboard when you have it it’s simple as:
$response = Http::withHeaders([
'Access-Token' => $access_token,
'Content-Type' => 'application/json',
])the URL that the request needs to be sent:
https://business-api.tiktok.com/open_api/v2.0/event/track/In the documentation is not exactly clear which payload syntax is tight to which version but using the 2.0 it the safe bet here.
$url = 'https://business-api.tiktok.com/open_api/v2.0/event/track/';
$payload = [
'event' => $event_name,
'event_time' => now()->getTimestamp(),
'user' => [
'ttclid' => $click_id,
'email' => hash('sha256', strtolower(trim($user->email))),
'external_id' => hash('sha256', (string) $user->id),
],
'properties' => [
'contents' => [
[
'content_name' => $plan,
'price' => $amount_paid,
'quantity' => 1,
],
],
'currency' => 'USD',
'content_type' => 'product',
],
'page' => [
'url' => config('app.url'),
],
];
$response = Http::withHeaders([
'Access-Token' => $access_token,
'Content-Type' => 'application/json',
])->post($url, $payload);if there are any issues with the payload you can use the helper they provided to validate the data that is sent it shows error messages regarding the payload.
Conclusion
As one of the most popular social media platforms TikTok is a great place to track you users engagements and using the both pixel and Conversion API is a power move.
One advice though: Don’t blindly trust their test events, as I learned the hard way it’s not working 100% of the times which was later confirmed from the TikTok Support.