From e88759ab32da882f785a07ea67bbd7d63847a100 Mon Sep 17 00:00:00 2001 From: greyvugrin Date: Thu, 23 Mar 2023 15:21:11 -0700 Subject: [PATCH 1/3] Update track and identify to support context --- src/PendingUserSegment.php | 13 +++++++++---- src/SegmentService.php | 13 +++++++++---- src/SimpleSegmentEvent.php | 6 ++++-- src/SimpleSegmentIdentify.php | 6 ++++-- src/ValueObjects/SegmentPayload.php | 8 +++++++- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/PendingUserSegment.php b/src/PendingUserSegment.php index 2868c79..012a4a4 100644 --- a/src/PendingUserSegment.php +++ b/src/PendingUserSegment.php @@ -16,17 +16,22 @@ public function __construct(SegmentService $service, CanBeIdentifiedForSegment $ $this->user = $user; } - public function track(string $event, ?array $eventData = null): void + public function track(string $event, ?array $eventData = null, ?array $context = null): void { $this->service->push( - new SimpleSegmentEvent($this->user, $event, $eventData) + new SimpleSegmentEvent( + $this->user, + $event, + $eventData, + $context + ) ); } - public function identify(?array $identifyData = null): void + public function identify(?array $identifyData = null, ?array $context = null): void { $this->service->push( - new SimpleSegmentIdentify($this->user, $identifyData) + new SimpleSegmentIdentify($this->user, $identifyData, $context) ); } } diff --git a/src/SegmentService.php b/src/SegmentService.php index ab62c27..aee52e7 100644 --- a/src/SegmentService.php +++ b/src/SegmentService.php @@ -35,17 +35,17 @@ public function setGlobalContext(array $globalContext): void $this->globalContext = $globalContext; } - public function track(string $event, ?array $eventData = null): void + public function track(string $event, ?array $eventData = null, ?array $context = null): void { $this->push( - new SimpleSegmentEvent($this->globalUser, $event, $eventData) + new SimpleSegmentEvent($this->globalUser, $event, $eventData, $context) ); } - public function identify(?array $identifyData = null): void + public function identify(?array $identifyData = null, ?array $context = null): void { $this->push( - new SimpleSegmentIdentify($this->globalUser, $identifyData) + new SimpleSegmentIdentify($this->globalUser, $identifyData, $context) ); } @@ -122,6 +122,11 @@ protected function getDataFromPayload(SegmentPayload $payload): array $data['event'] = $payload->getEvent(); } + // Add individual context. Global context is merged onto it. + if (! empty($payload->getContext())) { + $data['context'] = $payload->getContext(); + } + return $data; } diff --git a/src/SimpleSegmentEvent.php b/src/SimpleSegmentEvent.php index 8325355..809ff87 100644 --- a/src/SimpleSegmentEvent.php +++ b/src/SimpleSegmentEvent.php @@ -12,7 +12,8 @@ class SimpleSegmentEvent implements CanBeSentToSegment public function __construct( private CanBeIdentifiedForSegment $user, private string $event, - private ?array $eventData + private ?array $eventData, + private ?array $context = null ) { } @@ -21,7 +22,8 @@ public function toSegment(): SegmentPayload $payload = new SegmentPayload( $this->user, SegmentPayloadType::TRACK(), - $this->eventData + $this->eventData, + $this->context ); $payload->setEvent($this->event); diff --git a/src/SimpleSegmentIdentify.php b/src/SimpleSegmentIdentify.php index d6c719e..abdf9f0 100644 --- a/src/SimpleSegmentIdentify.php +++ b/src/SimpleSegmentIdentify.php @@ -11,7 +11,8 @@ class SimpleSegmentIdentify implements CanBeSentToSegment { public function __construct( private CanBeIdentifiedForSegment $user, - private ?array $identifyData = null + private ?array $identifyData = null, + private ?array $context = null ) { } @@ -20,7 +21,8 @@ public function toSegment(): SegmentPayload return new SegmentPayload( $this->user, SegmentPayloadType::IDENTIFY(), - $this->identifyData + $this->identifyData, + $this->context ); } } diff --git a/src/ValueObjects/SegmentPayload.php b/src/ValueObjects/SegmentPayload.php index f79d990..e5f4e50 100644 --- a/src/ValueObjects/SegmentPayload.php +++ b/src/ValueObjects/SegmentPayload.php @@ -19,7 +19,8 @@ class SegmentPayload public function __construct( protected CanBeIdentifiedForSegment $user, protected SegmentPayloadType $type, - protected ?array $data + protected ?array $data, + protected ?array $context = null ) { } @@ -38,6 +39,11 @@ public function getData(): ?array return $this->data; } + public function getContext(): ?array + { + return $this->context; + } + public function getEvent(): string { return $this->event; From ba019cd59347a84c57f4c1717de8046970708f26 Mon Sep 17 00:00:00 2001 From: greyvugrin Date: Fri, 24 Mar 2023 10:14:25 -0700 Subject: [PATCH 2/3] Add tests for context support --- tests/Unit/SegmentServiceTest.php | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/Unit/SegmentServiceTest.php b/tests/Unit/SegmentServiceTest.php index e6bd4c9..535db36 100644 --- a/tests/Unit/SegmentServiceTest.php +++ b/tests/Unit/SegmentServiceTest.php @@ -36,6 +36,10 @@ // When we call the track method Segment::track('Something Happened', [ 'name' => 'special', + ], [ + 'page' => [ + 'title' => 'Home', + ] ]); // Then we have made the calls to Segment @@ -53,6 +57,11 @@ "name" => "special", ], "event" => "Something Happened", + "context" => [ + "page" => [ + "title" => "Home", + ] + ] ]); }); }); @@ -81,6 +90,10 @@ // When we call the track method Segment::identify([ "has_confirmed_something" => true, + ], [ + 'page' => [ + 'title' => 'Home', + ] ]); // Then we have made the calls to Segment @@ -97,6 +110,11 @@ "traits" => [ "has_confirmed_something" => true, ], + "context" => [ + "page" => [ + "title" => "Home", + ] + ] ]); }); }); @@ -141,6 +159,10 @@ // When we call the track method Segment::forUser($user)->track('Something Happened', [ 'name' => 'special', + ], [ + 'page' => [ + 'title' => 'Home', + ] ]); // Then we have made the calls to Segment @@ -158,6 +180,11 @@ "name" => "special", ], "event" => "Something Happened", + "context" => [ + "page" => [ + "title" => "Home", + ] + ] ]); }); }); @@ -178,6 +205,10 @@ // When we call the track method Segment::forUser($user)->identify([ "has_confirmed_something" => true, + ], [ + 'page' => [ + 'title' => 'Home', + ] ]); // Then we have made the calls to Segment @@ -194,6 +225,11 @@ "traits" => [ "has_confirmed_something" => true, ], + "context" => [ + "page" => [ + "title" => "Home", + ] + ] ]); }); }); From 76514b27d7b2f3da60dafae77ccc30e41e36a067 Mon Sep 17 00:00:00 2001 From: greyvugrin Date: Fri, 24 Mar 2023 10:15:36 -0700 Subject: [PATCH 3/3] Update README to mention individual event context --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 97bd595..932c662 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,35 @@ Segment::setGlobalContext([ ]); ``` +### Setting context on a per event basis + +You can also set context on a per event basis. This will merge the context you provide with the global context. + +```php +use SlashEquip\LaravelSegment\Facades\Segment; + +// Identify user with context +Segment::forUser($user)->identify([ + 'name' => 'John Doe', +], [ + 'page' => [ + 'path' => '/signup', + 'referrer' => 'https://producthunt.com', + ], +]); + +// Track example event with context +Segment::forUser($user)->track('User Signed Up', [ + 'source' => 'Product Hunt', +], [ + 'page' => [ + 'path' => '/signup', + 'referrer' => 'https://producthunt.com', + ], +]); +``` + + ### Here have some convenience Laravel Segment ships with a middleware that you can apply in your HTTP Kernal that will handle