Skip to content

Commit c80bc2b

Browse files
committed
Added functionality to manipulate tracking data.
1 parent 066e037 commit c80bc2b

File tree

10 files changed

+127
-22
lines changed

10 files changed

+127
-22
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ This project adheres to [Semantic Versioning](http://semver.org/).
99
(This is already detected in subscription update.)
1010
- Filter any incoming web-hook events that are in test mode.
1111

12+
## [0.9.1] - 2019-10-18
13+
### Added
14+
- functionality to inject custom data points into track events.
15+
16+
### Fixed
17+
- Laravel 6.x compatibility.
18+
19+
## [0.9.0] - 2019-08-28
20+
### Added
21+
- Laravel 6.0 compatibility.
22+
1223
## [0.8.1] - 2019-07-28
1324
### Fixed
1425
- tracking of user information.

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ MIXPANEL_TOKEN=xxxxxxxxxxxxxxxxxxxxxx
6666
connections timeout.
6767
- `services.mixpanel.timeout`: (default: 2) set the number of seconds after which event tracking
6868
times out.
69+
- `services.mixpanel.data_callback_class`: (default: null) manipulate the data
70+
being passed back to mixpanel for the track events.
6971

7072
## Upgrade Notes
7173
### Version 0.7.0 for Laravel 5.5
@@ -232,6 +234,36 @@ the first name. Otherwise it will look for `first_name` and `last_name` fields i
232234
Session:
233235
- Status: Logged Out
234236
```
237+
### Tracking Data Manipulation
238+
If you need to make changes or additions to the data being tracked, create a
239+
class that implements `\GeneaLabs\LaravelMixpanel\Interfaces\DataCallback`:
240+
241+
```php
242+
<?php
243+
244+
namespace App;
245+
246+
use GeneaLabs\LaravelMixpanel\Interfaces\DataCallback;
247+
248+
class MixpanelUserData implements DataCallback
249+
{
250+
public function process(array $data = []) : array
251+
{
252+
$data["test"] = "value";
253+
254+
return $data;
255+
}
256+
}
257+
```
258+
259+
Then register this class in your `services` configuration:
260+
261+
```php
262+
'mixpanel' => [
263+
// ...
264+
"data_callback_class" => \App\MixpanelUserData::class,
265+
]
266+
```
235267

236268
### Stripe Integration
237269
Many L5 sites are running Cashier to manage their subscriptions. This package creates an API webhook endpoint that keeps

composer.json

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,28 @@
1414
}
1515
},
1616
"require": {
17-
"illuminate/auth": "5.5 - 6.0",
18-
"illuminate/config": "5.5 - 6.0",
19-
"illuminate/console": "5.5 - 6.0",
20-
"illuminate/events": "5.5 - 6.0",
21-
"illuminate/http": "5.5 - 6.0",
22-
"illuminate/routing": "5.5 - 6.0",
23-
"illuminate/queue": "5.5 - 6.0",
24-
"illuminate/support": "5.5 - 6.0",
25-
"mixpanel/mixpanel-php": "*",
26-
"sinergi/browser-detector": "*"
17+
"illuminate/auth": "^5.5|^6.0",
18+
"illuminate/config": "^5.5|^6.0",
19+
"illuminate/console": "^5.5|^6.0",
20+
"illuminate/events": "^5.5|^6.0",
21+
"illuminate/http": "^5.5|^6.0",
22+
"illuminate/routing": "^5.5|^6.0",
23+
"illuminate/queue": "^5.5|^6.0",
24+
"illuminate/support": "^5.5|^6.0",
25+
"mixpanel/mixpanel-php": "^2.7",
26+
"sinergi/browser-detector": "^6.1"
2727
},
2828
"require-dev": {
29-
"fzaninotto/faker": "*",
30-
"laravel/laravel": "dev-develop@dev",
31-
"laravel/browser-kit-testing": "*",
32-
"mockery/mockery": "*",
33-
"phpmd/phpmd": "*",
34-
"phpunit/phpunit": "*",
35-
"sebastian/phpcpd": "*",
36-
"symfony/thanks": "*",
37-
"php-coveralls/php-coveralls": "*"
29+
"fzaninotto/faker": "^1.8",
30+
"laravel/laravel": "^6.0",
31+
"laravel/browser-kit-testing": "^5.1",
32+
"mockery/mockery": "^1.2",
33+
"phpmd/phpmd": "^2.7",
34+
"phpunit/phpunit": "^8.4",
35+
"sebastian/phpcpd": "^4.1",
36+
"symfony/thanks": "^1.0",
37+
"php-coveralls/php-coveralls": "^2.1",
38+
"squizlabs/php_codesniffer": "^3.5"
3839
},
3940
"autoload-dev": {
4041
"psr-4": {

config/services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
'consumer' => 'socket',
88
'connect-timeout' => 2,
99
'timeout' => 2,
10+
"data_callback_class" => null,
1011
]
1112
];

phpmd.xml

Whitespace-only changes.

src/Interfaces/DataCallback.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace GeneaLabs\LaravelMixpanel\Interfaces;
4+
5+
interface DataCallback
6+
{
7+
public function process(array $data = []) : array;
8+
}

src/LaravelMixpanel.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace GeneaLabs\LaravelMixpanel;
1+
<?php
2+
3+
namespace GeneaLabs\LaravelMixpanel;
24

35
use Illuminate\Http\Request;
46
use Illuminate\Support\Facades\App;
@@ -9,18 +11,21 @@
911

1012
class LaravelMixpanel extends Mixpanel
1113
{
14+
private $callbackResults;
1215
private $defaults;
1316
private $request;
1417

1518
public function __construct(Request $request, array $options = [])
1619
{
20+
$this->callbackResults = [];
1721
$this->defaults = [
1822
'consumer' => config('services.mixpanel.consumer', 'socket'),
1923
'connect_timeout' => config('services.mixpanel.connect-timeout', 2),
2024
'timeout' => config('services.mixpanel.timeout', 2),
2125
];
2226
$this->request = $request;
2327

28+
2429
parent::__construct(
2530
config('services.mixpanel.token'),
2631
array_merge($this->defaults, $options)
@@ -58,7 +63,13 @@ protected function getData() : array
5863
public function track($event, $properties = [])
5964
{
6065
$properties = array_filter($properties);
66+
$data = $properties + $this->getData();
67+
68+
if ($callbackClass = config("services.mixpanel.data_callback_class")) {
69+
$data = (new $callbackClass)->process($data);
70+
$data = array_filter($data);
71+
}
6172

62-
parent::track($event, $properties + $this->getData());
73+
parent::track($event, $data);
6374
}
6475
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace GeneaLabs\LaravelMixpanel\Tests\Fixtures\App;
4+
5+
use GeneaLabs\LaravelMixpanel\Interfaces\DataCallback;
6+
7+
class MixpanelUserData implements DataCallback
8+
{
9+
public function process(array $data = []) : array
10+
{
11+
$data["test"] = "value";
12+
13+
return $data;
14+
}
15+
}

tests/Unit/DataCallbackTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace GeneaLabs\LaravelMixpanel\Tests\Unit;
4+
5+
use GeneaLabs\LaravelMixpanel\Tests\Fixtures\App\MixpanelUserData;
6+
use GeneaLabs\LaravelMixpanel\Tests\UnitTestCase;
7+
8+
class DataCallbackTest extends UnitTestCase
9+
{
10+
public function testDataCallbackClassReturnsArray()
11+
{
12+
$data = (new MixpanelUserData)->process();
13+
14+
$this->assertIsArray($data);
15+
}
16+
17+
public function testDataCallbackArrayContainsValue()
18+
{
19+
$data = (new MixpanelUserData)->process();
20+
21+
$this->assertArrayHasKey("test", $data);
22+
$this->assertEquals("value", $data["test"]);
23+
}
24+
}

tests/Unit/Facades/MixpanelTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace GeneaLabs\LaravelMixpanel\Tests\Unit\Facades;
1+
<?php
2+
3+
namespace GeneaLabs\LaravelMixpanel\Tests\Unit\Facades;
24

35
use GeneaLabs\LaravelMixpanel\Facades\Mixpanel;
46
use GeneaLabs\LaravelMixpanel\LaravelMixpanel;

0 commit comments

Comments
 (0)