Skip to content

Commit

Permalink
Merge pull request #1 from placetopay-org/feature/update-mixpanel
Browse files Browse the repository at this point in the history
Feature/update mixpanel
  • Loading branch information
kevinvargasl authored Apr 26, 2024
2 parents b145972 + 5520329 commit c1767e7
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 56 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.0.0] - 2024-04-23
## [1.0.0] - 2024-04-25

### Added

Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ This package can be installed via composer:
composer require "placetopay/analytics-tracker"
```

## Supported analytics trackers

- [Mixpanel](https://mixpanel.com/)

## Usage

Add the following variables to your .env:
Expand All @@ -23,10 +27,10 @@ Add the following variables to your .env:
- MIXPANEL_PROJECT_TOKEN={{token}}

```php
use Placetopay\AnalyticsTracker\Contracts\Tracker;
use Placetopay\AnalyticsTracker\Contracts\AnalyticsTracker;

app(Tracker::class)
->identify("user@company.com") // Associate a user to the tracked events
->setDefaultPayload(['key' => 'value']) // Set the default data to be sent on every event track
app(AnalyticsTracker::class)
->setIdentifier("user@company.com") // (optional) Associate a user to the tracked events
->setDefaultPayload(['key' => 'value']) // Set the default data to be sent on every track call
->track('Label', ['key' => 'value']); // Tracks an event
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Placetopay\AnalyticsTracker\Contracts;

interface Tracker
interface AnalyticsTracker
{
public function track(string $label, array $payload = []);

public function setDefaultPayload(array $payload): self;

public function identify(string $identifier): self;
public function setIdentifier(string $identifier): self;
}
6 changes: 3 additions & 3 deletions src/Providers/TrackerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use Placetopay\AnalyticsTracker\Contracts\Tracker;
use Placetopay\AnalyticsTracker\Contracts\AnalyticsTracker;
use Placetopay\AnalyticsTracker\Trackers\MixpanelTracker;

class TrackerServiceProvider extends ServiceProvider implements DeferrableProvider
{
public function register(): void
{
$this->app->singleton(Tracker::class, MixpanelTracker::class);
$this->app->singleton(AnalyticsTracker::class, MixpanelTracker::class);
}

public function provides(): array
{
return [Tracker::class];
return [AnalyticsTracker::class];
}
}
30 changes: 8 additions & 22 deletions src/Trackers/MixpanelTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,30 @@
namespace Placetopay\AnalyticsTracker\Trackers;

use Mixpanel;
use Placetopay\AnalyticsTracker\Contracts\Tracker;
use Placetopay\AnalyticsTracker\Contracts\AnalyticsTracker;

class MixpanelTracker implements Tracker
class MixpanelTracker implements AnalyticsTracker
{
public ?Mixpanel $mixpanel = null;
private bool $identified = false;
private ?Mixpanel $mixpanel = null;
private array $defaultPayload = [];

public function __construct()
{
if (!$this->enabled()) {
logger()->warning('Calling mixpanel but it is disabled');
return;
}

if (!$this->hasToken()) {
if (!$token = config('analytics-tracker.mixpanel.project_token')) {
logger()->warning('Calling mixpanel but there is not token');
return;
}

$this->mixpanel = Mixpanel::getInstance(config('analytics-tracker.mixpanel.project_token'));
$this->mixpanel = app(Mixpanel::class, ['token' => $token]);
}

public function track(string $label, array $payload = []): void
{
if (!$this->identified) {
logger()->warning('Event tracked but user has not been identified');
}

$this->mixpanel?->track($label, array_merge($this->defaultPayload, $payload, ['backend' => true]));
$this->mixpanel?->track($label, array_merge($this->defaultPayload, $payload));
}

public function setDefaultPayload(array $payload): self
Expand All @@ -46,17 +40,9 @@ private function enabled(): bool
return config('analytics-tracker.mixpanel.enabled');
}

public function hasToken(): bool
{
return (bool)config('analytics-tracker.mixpanel.project_token');
}

public function identify(string $identifier): self
public function setIdentifier(string $identifier): self
{
if (!$this->identified) {
$this->mixpanel?->identify(strtolower(trim($identifier)));
$this->identified = true;
}
$this->mixpanel?->identify(strtolower(trim($identifier)));

return $this;
}
Expand Down
70 changes: 46 additions & 24 deletions tests/Unit/Trackers/MixpanelTrackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,27 @@ protected function setUp(): void
*/
public function it_can_track_an_event(): void
{
$key = $this->faker->word();
$value = $this->faker->word();

$eventLabel = $this->faker->word();

$testPayload = [
$this->faker->word(),
'backend' => true,
'customProperty' => 'johnDue@user.com',
];

$mixpanelMock = $this->createMock(Mixpanel::class);
$mixpanelMock->expects($this->once())
->method('track')
->with(
$this->equalTo($eventLabel),
$this->equalTo(['backend' => true, $key => $value])
$this->equalTo($testPayload)
);
$this->app->offsetSet(Mixpanel::class, $mixpanelMock);

$mixpanelTracker = new MixpanelTracker();
$mixpanelTracker->mixpanel = $mixpanelMock;
$mixpanelTracker->setDefaultPayload([$key => $value]);
$mixpanelTracker->identify($this->faker->email());
$mixpanelTracker->track($eventLabel);
$mixpanelTracker
->setDefaultPayload($testPayload)
->track($eventLabel);
}

/**
Expand All @@ -51,14 +54,15 @@ public function it_can_track_an_event(): void
public function it_can_identify_the_user(): void
{
$fakeEmail = $this->faker->email();

$mixpanelMock = $this->createMock(Mixpanel::class);
$mixpanelMock->expects($this->once())
->method('identify')
->with($fakeEmail);
$this->app->offsetSet(Mixpanel::class, $mixpanelMock);

$mixpanelTracker = new MixpanelTracker();
$mixpanelTracker->mixpanel = $mixpanelMock;
$mixpanelTracker->identify($fakeEmail);
$mixpanelTracker->setIdentifier($fakeEmail);
}

/**
Expand All @@ -71,32 +75,50 @@ public function it_can_track_if_user_has_not_been_identified(): void
$mixpanelMock = $this->createMock(Mixpanel::class);
$mixpanelMock->expects($this->once())
->method('track')
->with($label);

Log::shouldReceive('warning')->once();
->with($label);
$this->app->offsetSet(Mixpanel::class, $mixpanelMock);

$mixpanelTracker = new MixpanelTracker();
$mixpanelTracker->mixpanel = $mixpanelMock;
$mixpanelTracker->track($label);
}

/**
* @test
* @dataProvider configProvider
*/
public function it_does_not_initialize_mixpanel_when_disabled_or_has_no_token($setting, $value): void
public function it_does_not_track_when_disabled(): void
{
config()->set($setting, $value);
Log::shouldReceive('warning')->once();
config()->set('analytics-tracker.mixpanel.enabled', false);

$label = $this->faker->word();

$mixpanelMock = $this->createMock(Mixpanel::class);
$mixpanelMock->expects($this->never())
->method('track')
->with($label);
$this->app->offsetSet(Mixpanel::class, $mixpanelMock);

$mixpanelTracker = new MixpanelTracker();
$this->assertNull($mixpanelTracker->mixpanel);
$mixpanelTracker->track($label);
}

public static function configProvider(): array
/**
* @test
*/
public function it_does_not_track_when_has_no_token(): void
{
return [
['analytics-tracker.mixpanel.project_token', null],
['analytics-tracker.mixpanel.enabled', false],
];
config()->set('analytics-tracker.mixpanel.project_token', '');

$label = $this->faker->word();

$mixpanelMock = $this->createMock(Mixpanel::class);
$mixpanelMock->expects($this->never())
->method('track')
->with($label);
$this->app->offsetSet(Mixpanel::class, $mixpanelMock);

Log::shouldReceive('warning')->with('Calling mixpanel but there is not token')->once();

$mixpanelTracker = new MixpanelTracker();
$mixpanelTracker->track($label);
}
}

0 comments on commit c1767e7

Please sign in to comment.