Skip to content

Commit

Permalink
Added event-data/events + event-data/fields
Browse files Browse the repository at this point in the history
  • Loading branch information
marcfowler committed May 3, 2024
1 parent fd316ce commit fcda498
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 20 deletions.
60 changes: 43 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ and then add these to your `.env` file:
UMAMI_URL=https://stats-site-com/api
UMAMI_USERNAME=username
UMAMI_PASSWORD="password"
UMAMI_WEBSITE_ID=d131d2ae-5d21-4a54-80ba-16719afedf7b
```

## Usage

### Query Stats
```php
\Umami\Umami::query(2,'metrics',[
'start_at'=>today()->subDays(7),
'end_at'=>today(),
'type'=>'referrer',
]);
\Umami\Umami::query('example-site-id', 'metrics', [
'start_at'=>today()->subDays(7),
'end_at'=>today(),
'type'=>'referrer',
]);
```

<br>
Expand All @@ -74,7 +75,7 @@ short usage for `PHP 8` to get default stats for the last 7 days and without cac
required: site id from umami server

```php
\Umami\Umami::query(siteID);
\Umami\Umami::query('example-site-id');
```

#### Part
Expand All @@ -86,7 +87,7 @@ available options : `active, stats, pageviews, events, metrics`
default: `stats`

```php
\Umami\Umami::query(siteID,'pageviews');
\Umami\Umami::query('example-site-id','pageviews');
```

### Options for Query Stats
Expand All @@ -100,7 +101,7 @@ default: last 7 days
you can pass `carbon` object or timestamp in milliseconds

```php
\Umami\Umami::query(siteID,'metrics',[
\Umami\Umami::query('example-site-id','metrics',[
'startAt'=>today()->subDays(7),
'endAt'=>now(),
]);
Expand All @@ -114,7 +115,7 @@ optional: Time unit, available options: `year, month, hour, day`,
default: day

```php
\Umami\Umami::query(siteID,'metrics',[
\Umami\Umami::query('example-site-id','metrics',[
'unit'=>'year',
]);
```
Expand All @@ -127,7 +128,7 @@ only available on `pageviews` and `events`
default: config('app.timezone')

```php
\Umami\Umami::query(siteID,'metrics',[
\Umami\Umami::query('example-site-id','metrics',[
'tz'=>'America/Los_Angeles',
]);
```
Expand All @@ -141,7 +142,7 @@ available options: `url, referrer, browser, os, device, country, event`,
default: url

```php
\Umami\Umami::query(siteID,'metrics',[
\Umami\Umami::query('example-site-id','metrics',[
'tz'=>'America/Los_Angeles',
]);
```
Expand All @@ -164,8 +165,8 @@ default: url
```

#### create a website for a diffrent user
if you want to create a website for different user then the admin user defined in the config, you need to provide the authentication for that user.
this is helpfully when creating a new user with a website
If you want to create a website for different user then the admin user defined in the config, you need to provide the authentication for that user.
this is helpful when creating a new user with a website

```php
\Umami\Umami::createWebsite([
Expand All @@ -180,20 +181,45 @@ this is helpfully when creating a new user with a website
### Update a website

```php
\Umami\Umami::updateWebsites('d131d2ae-5d21-4a54-80ba-16719afedf7b',[
\Umami\Umami::updateWebsites('example-site-id',[
'name'=>'user name',
]);
```

### Delete a website

```php
\Umami\Umami::deleteWebsite('d131d2ae-5d21-4a54-80ba-16719afedf7b');
\Umami\Umami::deleteWebsite('example-site-id');
```

## Event data

### Get website event and field key record counts within a given time range

See the API documentation for details: https://umami.is/docs/api/event-data#get-/api/event-data/events

```php
\Umami\Umami::events('example-site-id', [
'startAt'=>today()->subDays(7),
'endAt'=>now(),
'event' => 'custom_event',
]);
```

### Get website field key and value record counts within a given time range

See the API documentation for details: https://umami.is/docs/api/event-data#get-/api/event-data/fields

```php
\Umami\Umami::event_fields('example-site-id', [
'startAt'=>today()->subDays(7),
'endAt'=>now(),
]);
```

## Users

### Get All users
### Get all users

```php
\Umami\Umami::users();
Expand All @@ -214,7 +240,7 @@ this is helpfully when creating a new user with a website
]);
```

### Delete an user
### Delete a user

```php
\Umami\Umami::deleteUser(2);
Expand Down
9 changes: 7 additions & 2 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

return [
/**
* the server URL with the /api.
* the server URL, including the trailing `/api`
*/
'url' => env('UMAMI_URL', null),

Expand All @@ -18,13 +18,18 @@
*/
'password' => env('UMAMI_PASSWORD', null),

/**
* default website ID on your umami server.
*/
'website_id' => env('UMAMI_WEBSITE_ID', null),

/**
* default cache key.
*/
'cache_key' => 'umami.stats',

/**
* cache key
* cache ttl
* \DateTimeInterface|\DateInterval|int|null.
*/
'cache_ttl' => Carbon::parse('1 day'),
Expand Down
48 changes: 47 additions & 1 deletion src/Umami.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function auth(array $authData = null): ?string
abort_if(
config('umami.url') === null ||
config('umami.username') === null ||
config('umami.password') === null, 421, 'please make sur to set all umami config');
config('umami.password') === null, 421, 'Please make sure to set all the required Umami configuration values.');

if ($authData === null) {
$authData = [
Expand Down Expand Up @@ -64,6 +64,46 @@ public static function query(string $siteID, string $part = 'stats', array $opti
});
}

public static function events(string $siteID, array $options = null, bool $force = false, $authData = null): mixed {
$part = 'event-data-events';

$options = self::setOptions($part, $options);
$options['websiteId'] = $siteID;

$response = Http::withToken(self::auth($authData))
->get(config('umami.url') . '/event-data/events', $options);

$response->throw();

if ($force) {
cache()->forget(config('umami.cache_key').'.'.$siteID.'.'.$part);
}

return cache()->remember(config('umami.cache_key').'.'.$siteID.'.'.$part, config('umami.cache_ttl'), function () use ($response) {
return $response->json();
});
}

public static function event_fields(string $siteID, array $options = null, bool $force = false, $authData = null): mixed {
$part = 'event-data-fields';

$options = self::setOptions($part, $options);
$options['websiteId'] = $siteID;

$response = Http::withToken(self::auth($authData))
->get(config('umami.url') . '/event-data/fields', $options);

$response->throw();

if ($force) {
cache()->forget(config('umami.cache_key').'.'.$siteID.'.'.$part);
}

return cache()->remember(config('umami.cache_key').'.'.$siteID.'.'.$part, config('umami.cache_ttl'), function () use ($response) {
return $response->json();
});
}

/**
* set the defaults options for the $part.
*/
Expand Down Expand Up @@ -118,6 +158,12 @@ private static function setOptions($part, $options): array
'city' => null,
],
'active' => [],
'event-data-events' => [
'event' => null,
],
'event-data-fields' => [
//
],
];

$datesOptions = [
Expand Down

0 comments on commit fcda498

Please sign in to comment.