kregel / tamber
Tamber API 的 PHP SDK。
v1.0.0
2019-08-12 19:10 UTC
Requires
- php: >=5.6
- ext-json: *
- guzzlehttp/guzzle: ^6.2
This package is auto-updated.
Last update: 2024-09-13 05:50:45 UTC
README
这是一个非官方的 SDK。
通过创建账户来获取您的 API Token
首先要做的是前往官方 Tamber 网站进行注册
下载项目
当此包最终在 Packagist 上时,您可以使用以下方式安装它
composer require kregel/tamber
此包的使用
事件
<?php require 'vendor/autoload.php'; use Kregel\Tamber\Tamber; use Kregel\Tamber\Event; Tamber::setProjectKey('...'); Tamber::setEngineKey('...'); // If you just created your account you won't be able to set or create an engine until you track at least 1 event. try { $response = (new Event)->track([ /** * This will be created if it doesn't exist by default (via Tamber's code not this package) */ 'user' => 'your user id using whatever format you want', /** * This can be anything from likes, dislikes, purchases, clicks, reads, ect. * This will be created if it doesn't exist by default (via Tamber's code not this package) */ 'behavior' => 'like', /** * If I were using Laravel I might do something like `App\User:1` or `App\Models\Transaction:810`. Something to signify the thing performing * that's performing said beahvior and the identifier to track that thing's previous behaviors. */ 'item' => 'spotify:track:1JIgaRnqtzS7DuGM3hVZU9', /** * The Tamber docs mention the context could be related to A/B testing for interface changes. * but it could also be used to track the user's current url, previous things the user clicked on or other actions that the user preformed * like whether or not they played the song or read the book. */ 'context' => [ 'home-page', ], /** * This is just to indicate whether or not this specific behavior was related to a recommened/suggested action. * i.e. Did they play the song because it was in your recommended list? */ 'hit' => false, ]); print_r($response->getContents()); } catch (\Kregel\Tamber\Exceptions\TamberException $e) { echo $e->getMessage() . "\n"; print_r($e->getContext()); }
行为
<?php use Kregel\Tamber\Tamber; use Kregel\Tamber\Behavior; Tamber::setProjectKey($projectToken); try { $behavior = (new Behavior)->create([ 'name' => 'purchase', 'desirability' => 0.6 ]); } catch (\Kregel\Tamber\Exceptions\TamberException $e) { echo $e->getMessage() . "\n"; print_r($e->getContext()); }
项目
创建
<?php use Kregel\Tamber\Tamber; use Kregel\Tamber\Item; Tamber::setProjectKey($projectToken); try { $item = (new Item)->create([ 'id' => 'App\Song:1', // This just needs to be some kind of unique identifier. 'properties' => [ 'artist' => 'Logic', 'title' => 'Under Pressure', 'length' => '9:20', 'explicit' => true ], 'tags' => [ 'rap', 'hip-hop' ], 'created' => App\Song::find(1)->created_at // This is just to represent when the song was created in your system. ]); } catch (\Kregel\Tamber\Exceptions\TamberException $e) { echo $e->getMessage() . "\n"; print_r($e->getContext()); }
获取项目
<?php use Kregel\Tamber\Tamber; use Kregel\Tamber\Item; Tamber::setProjectKey($projectToken); $item = (new Item)->retrieve([ 'id' => 'spotify:track:5LME7YULt0enp6UAB8VoDn' ]);
旁注
需要注意的是,Tamber API 中的每个项目都使用 TransformRequest
特性。这意味着我们拦截了对 SDK 的调用,并将它们转换成更易于使用的形式。
使用上面提到的行为创建示例。您可以通过简单地尝试从行为变量中访问它来访问任何响应数据。
$behavior->result->name // purchase $behavior->status // 200 $behavior->success // true // ect...