Skip to content

Commit 2b04944

Browse files
authored
Merge pull request #69 from phiHero/feat/analytics-events
Add support for analytics events
2 parents f88d50f + 1d34282 commit 2b04944

File tree

6 files changed

+136
-22
lines changed

6 files changed

+136
-22
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
runs-on: ubuntu-latest
99
services:
1010
typesense:
11-
image: typesense/typesense:26.0
11+
image: typesense/typesense:27.0.rc21
1212
ports:
1313
- 8108:8108/tcp
1414
volumes:

docker-compose.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
version: '3.5'
22

33
services:
4-
typesense:
5-
image: typesense/typesense:26.0
6-
environment:
7-
TYPESENSE_DATA_DIR: /data
8-
TYPESENSE_API_KEY: xyz
9-
volumes:
10-
- /tmp/typesense-server-data:/data
11-
ports:
12-
- 8108:8108
13-
restart: "no"
4+
typesense:
5+
image: typesense/typesense:27.0.rc21
6+
environment:
7+
TYPESENSE_DATA_DIR: /data
8+
TYPESENSE_API_KEY: xyz
9+
volumes:
10+
- /tmp/typesense-server-data:/data
11+
ports:
12+
- 8108:8108
13+
restart: 'no'

src/Analytics.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Analytics
1010

1111
private AnalyticsRules $rules;
1212

13+
private AnalyticsEvents $events;
14+
1315
public function __construct(ApiCall $apiCall)
1416
{
1517
$this->apiCall = $apiCall;
@@ -22,4 +24,12 @@ public function rules()
2224
}
2325
return $this->rules;
2426
}
27+
28+
public function events()
29+
{
30+
if (!isset($this->events)) {
31+
$this->events = new AnalyticsEvents($this->apiCall);
32+
}
33+
return $this->events;
34+
}
2535
}

src/AnalyticsEvents.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
/**
6+
* Class AnalyticsEvents
7+
*
8+
* @package \Typesense
9+
*/
10+
class AnalyticsEvents
11+
{
12+
const RESOURCE_PATH = '/analytics/events';
13+
14+
/**
15+
* @var ApiCall
16+
*/
17+
private ApiCall $apiCall;
18+
19+
/**
20+
* AnalyticsEvents constructor.
21+
*
22+
* @param ApiCall $apiCall
23+
*/
24+
public function __construct(ApiCall $apiCall)
25+
{
26+
$this->apiCall = $apiCall;
27+
}
28+
29+
/**
30+
* @param array $params
31+
*
32+
* @return array
33+
* @throws TypesenseClientError|HttpClientException
34+
*/
35+
public function create($params)
36+
{
37+
return $this->apiCall->post($this->endpoint_path(), $params);
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
private function endpoint_path($operation = null)
44+
{
45+
return self::RESOURCE_PATH . ($operation === null ? '' : "/$operation");
46+
}
47+
}

tests/Feature/AnalyticsEventsTest.php

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,66 @@
66

77
class AnalyticsEventsTest extends TestCase
88
{
9-
//* there is no method for sending events
10-
// public function testCanCreateAnEvent(): void
11-
// {
12-
// $returnData = $this->client()->analytics->rules()->even
13-
// $this->assertEquals($returnData['name'], $this->ruleName);
14-
// }
9+
private $ruleName = 'product_queries_aggregation';
1510

16-
public function testNeedImplementationForAnalyticsEvents(): void
11+
protected function setUp(): void
1712
{
18-
$this->markTestIncomplete(
19-
'This test has not been implemented yet.',
20-
);
13+
parent::setUp();
14+
$this->client()->collections->create([
15+
"name" => "products",
16+
"fields" => [
17+
[
18+
"name" => "title",
19+
"type" => "string"
20+
],
21+
[
22+
"name" => "popularity",
23+
"type" => "int32",
24+
"optional" => true
25+
]
26+
]
27+
]);
28+
$this->client()->analytics->rules()->upsert($this->ruleName, [
29+
"name" => "products_popularity",
30+
"type" => "counter",
31+
"params" => [
32+
"source" => [
33+
"collections" => [
34+
"products"
35+
],
36+
"events" => [
37+
[
38+
"type" => "click",
39+
"weight" => 1,
40+
"name" => "products_click_event"
41+
]
42+
]
43+
],
44+
"destination" => [
45+
"collection" => "products",
46+
"counter_field" => "popularity"
47+
]
48+
]
49+
]);
50+
}
51+
52+
protected function tearDown(): void
53+
{
54+
parent::tearDown();
55+
$this->client()->analytics->rules()->{'product_queries_aggregation'}->delete();
56+
}
57+
58+
public function testCanCreateAnEvent(): void
59+
{
60+
$response = $this->client()->analytics->events()->create([
61+
"type" => "click",
62+
"name" => "products_click_event",
63+
"data" => [
64+
"q" => "nike shoes",
65+
"doc_id" => "1024",
66+
"user_id" => "111112"
67+
]
68+
]);
69+
$this->assertTrue($response['ok']);
2170
}
2271
}

tests/Feature/AnalyticsRulesTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class AnalyticsRulesTest extends TestCase
99
{
10-
private $ruleName = 'product_queries_aggregation';
10+
private $ruleName = 'test_rule';
1111
private $ruleConfiguration = [
1212
"type" => "popular_queries",
1313
"params" => [
@@ -29,6 +29,14 @@ protected function setUp(): void
2929
$this->ruleUpsertResponse = $this->client()->analytics->rules()->upsert($this->ruleName, $this->ruleConfiguration);
3030
}
3131

32+
protected function tearDown(): void
33+
{
34+
$rules = $this->client()->analytics->rules()->retrieve();
35+
foreach ($rules['rules'] as $rule) {
36+
$this->client()->analytics->rules()->{$rule['name']}->delete();
37+
}
38+
}
39+
3240
public function testCanUpsertARule(): void
3341
{
3442
$this->assertEquals($this->ruleName, $this->ruleUpsertResponse['name']);

0 commit comments

Comments
 (0)