Skip to content

Commit

Permalink
Merge pull request #15 from still-code/accounts
Browse files Browse the repository at this point in the history
support Accounts
  • Loading branch information
atmonshi authored Apr 17, 2023
2 parents c3066b5 + 6c8a026 commit e7d3f9c
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 29 deletions.
110 changes: 88 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ API wrapper for umami website analytics. get your statistics in the laravel app.

check out [Umami](https://umami.is/) own your website analytics

## Features

- Manage websites
- Manage accounts
- query statistics stats, page views, events and metrics

## Installation

You can install the package via composer:
Expand All @@ -41,34 +47,31 @@ UMAMI_PASSWORD="password"

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

#### short usage for `PHP 8` to get default stats for the last 7 days and without cache:
```php
$stats = \Umami\Umami::query(siteID: 1, force: false)
```
### Get All websites
<br>

short usage for `PHP 8` to get default stats for the last 7 days and without cache:
```php
$sites = \Umami\Umami::websites();
\Umami\Umami::query(siteID: 1, force: true)
```

## Parameters
### Parameters

### Site id
#### Site id

required: site id from umami server

```php
$stats = \Umami\Umami::query(siteID);
\Umami\Umami::query(siteID);
```

### Part
#### Part

required: the stats part you want to get from umami,

Expand All @@ -77,12 +80,12 @@ available options : `stats, pageviews, events, metrics`
default: `stats`

```php
$stats = \Umami\Umami::query(siteID,'pageviews');
\Umami\Umami::query(siteID,'pageviews');
```

## Options
### Options for Query Stats

### Dates (start_at,end_at)
#### Dates (start_at,end_at)

optional: Timestamp of starting and end date,

Expand All @@ -91,39 +94,39 @@ default: last 7 days
you can pass `carbon` object or timestamp in milliseconds

```php
$stats = \Umami\Umami::query(siteID,'metrics',[
\Umami\Umami::query(siteID,'metrics',[
'start_at'=>today()->subDays(7),
'end_at'=>today(),
]);
```

### unit
##### unit
only available on `pageviews` and `events`

optional: Time unit, available options: `year, month, hour, day`,

default: day

```php
$stats = \Umami\Umami::query(siteID,'metrics',[
\Umami\Umami::query(siteID,'metrics',[
'unit'=>'year',
]);
```

### Timezone (tz)
##### Timezone (tz)
optional: Timezone,

only available on `pageviews` and `events`

default: config('app.timezone')

```php
$stats = \Umami\Umami::query(siteID,'metrics',[
\Umami\Umami::query(siteID,'metrics',[
'tz'=>'America/Los_Angeles',
]);
```

### type (for metrics only)
##### type (for metrics only)

optional: Gets metrics for a given time range,

Expand All @@ -132,14 +135,77 @@ available options: `url, referrer, browser, os, device, country, event`,
default: url

```php
$stats = \Umami\Umami::query(siteID,'metrics',[
\Umami\Umami::query(siteID,'metrics',[
'tz'=>'America/Los_Angeles',
]);
```

## Websites

### Get All websites

```php
\Umami\Umami::websites();
```

### Create a website

```php
\Umami\Umami::createWebsite([
'domain'=>'domain.ltd',
'name'=>'account name',
'userId'=>'3', // account id
]);
```

### Update a website

```php
\Umami\Umami::websites('d131d2ae-5d21-4a54-80ba-16719afedf7b',[
'domain'=>'domain.ltd',
'name'=>'account name',
'userId'=>'3', // account id
]);
```

### Delete a website

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

## Accounts

### Get All accounts

```php
\Umami\Umami::accounts();
```

### Create an account

```php
\Umami\Umami::createAccount('username','password');
```

### Update an account

```php
\Umami\Umami::updateAccount(1,[
'username'=>'username',
'password'=>'password',
]);
```

### Delete an account

```php
\Umami\Umami::deleteAccounts(2);
```

## More details

Please checkout [Umami website](https://umami.is/) for more information.
Please check out [Umami website](https://umami.is/) for more information.

## Changelog

Expand Down
70 changes: 70 additions & 0 deletions src/Accounts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Umami;

use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;

trait Accounts
{
/**
* @throws RequestException
*/
public static function accounts(bool $force = false): mixed
{
$response = Http::withToken(session('umami_token'))
->get(config('umami.url').'/accounts');

$response->throw();

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

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

/**
* @throws RequestException
*/
public static function createAccount(string $username, string $password): mixed
{
$response = Http::withToken(session('umami_token'))
->post(config('umami.url').'/accounts', [
'password' => $password,
'username' => $username,
]);

$response->throw();

return $response->json();
}

/**
* @throws RequestException
*/
public static function updateAccount(string $userId, array $data): mixed
{
$response = Http::withToken(session('umami_token'))
->post(config('umami.url').'/accounts/'.$userId, $data);

$response->throw();

return $response->json();
}

/**
* @throws RequestException
*/
public static function deleteAccount($userId): mixed
{
$response = Http::withToken(session('umami_token'))
->delete(config('umami.url').'/accounts/'.$userId);

$response->throw();

return $response->json();
}
}
1 change: 1 addition & 0 deletions src/Umami.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Umami
{
use Websites;
use Accounts;

/**
* authenticate the user with umami stats' server.
Expand Down
9 changes: 2 additions & 7 deletions src/Websites.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,10 @@ public static function websites(bool $force = false): mixed
*
* @throws RequestException
*/
public static function createWebsite(string $domain, string $name, bool $share = false, bool $public = false): mixed
public static function createWebsite(array $data): mixed
{
$response = Http::withToken(session('umami_token'))
->post(config('umami.url').'/websites', [
'domain' => $domain,
'name' => $name,
'share' => $share,
'public' => $public,
]);
->post(config('umami.url').'/websites', $data);

$response->throw();

Expand Down

0 comments on commit e7d3f9c

Please sign in to comment.