msaaqcom / tiktok-php-sdk

TikTok Events API PHP Wrapper
MIT License
5 stars 5 forks source link

setTestEventCode should be optional, but it isn't #1

Open devcflynn opened 9 months ago

devcflynn commented 9 months ago

In the docs, it says that setTestEventCode is optional, but when removed, it returns error stating that test_event_code cannot be null. Does it make sense to add that to the call only if its provided? If I remove setTestEventCode like so, it throws an error requiring the code.

This does not work, but should if its optional:

$eventRequest = $tiktok->events()
            ->setEventSource(\Msaaq\TikTok\Enums\EventSource::WEB);
            //->setTestEventCode('TEST01234');
FrozenGod commented 2 weeks ago

I spent a whole day trying to figure it out, and since this was not fixed for almost a year now here's a solution I used to patch it:

  1. Install composer require cweagans/composer-patches:~2.0

  2. Create a file in your project root -> patches/msaaq/tiktok-php-sdk/optional-test-event-code.patch with the following content:

Index: src/Requests/EventRequest.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/Requests/EventRequest.php b/src/Requests/EventRequest.php
--- a/src/Requests/EventRequest.php 
+++ b/src/Requests/EventRequest.php (date 1729890208886)
@@ -53,14 +53,19 @@
         foreach ($events as $key => $event) {
             $events[$key] = $event->toArray();
         }
-
-        $request = $this->http->post('/event/track/', [
+        
+        $requestData = [
             'event_source' => $this->event_source->value,
             'event_source_id' => $this->event_source_id,
-            'test_event_code' => $this->test_event_code,
-
+            
             'data' => $events,
-        ]);
+        ];
+        
+        if ($this->test_event_code !== null) {
+            $requestData['test_event_code'] = $this->test_event_code;
+        }
+        
+        $request = $this->http->post('/event/track/', $requestData);

         $request->onError(function ($request) {
             throw new \Exception($request->json('message'));
  1. Add this to your composer.json:

    "patches": {
      "msaaq/tiktok-php-sdk": {
        "Make test_event_code optional": "patches/msaaq/tiktok-php-sdk/optional-test-event-code.patch"
      }
    }
  1. In terminal:

    cd vendor/msaaq/tiktok-php-sdk
    patch -p1 < ../../../patches/msaaq/tiktok-php-sdk/optional-test-event-code.patch
  2. If it works, the patch will be automatically applied when you run composer install or composer update from now on. Since you already applied it manually above (to debug) it will not be re-applied but the procedure remains the same for other patches.

Note: in order to easily make patches in the future you can use PHPStorm. Make changes to the vendor file you want to change, open local file history (PHPStorm feature) and there will be a patch button which will automatically create the patch content for you.

Hope that helps. Really unfortunate when such an easy fix to a very critical issue is not applied by the team who built it, but hey, this is why we use Github.