Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions src/PendingUserSegment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}
13 changes: 9 additions & 4 deletions src/SegmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 4 additions & 2 deletions src/SimpleSegmentEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/SimpleSegmentIdentify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand All @@ -20,7 +21,8 @@ public function toSegment(): SegmentPayload
return new SegmentPayload(
$this->user,
SegmentPayloadType::IDENTIFY(),
$this->identifyData
$this->identifyData,
$this->context
);
}
}
8 changes: 7 additions & 1 deletion src/ValueObjects/SegmentPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand All @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions tests/Unit/SegmentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,6 +57,11 @@
"name" => "special",
],
"event" => "Something Happened",
"context" => [
"page" => [
"title" => "Home",
]
]
]);
});
});
Expand Down Expand Up @@ -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
Expand All @@ -97,6 +110,11 @@
"traits" => [
"has_confirmed_something" => true,
],
"context" => [
"page" => [
"title" => "Home",
]
]
]);
});
});
Expand Down Expand Up @@ -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
Expand All @@ -158,6 +180,11 @@
"name" => "special",
],
"event" => "Something Happened",
"context" => [
"page" => [
"title" => "Home",
]
]
]);
});
});
Expand All @@ -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
Expand All @@ -194,6 +225,11 @@
"traits" => [
"has_confirmed_something" => true,
],
"context" => [
"page" => [
"title" => "Home",
]
]
]);
});
});
Expand Down