├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json └── src └── FathomAnalytics.php /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `fathom-api` will be documented in this file 4 | 5 | ## 1.0.0 - 201X-XX-XX 6 | 7 | - initial release 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Beyond Code GmbH 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The unofficial Fathom Analytics API 2 | 3 | This is an unofficial PHP SDK to get statistics out of Fathom Analytics, as long as it does not offer an official API. 4 | 5 | ## Installation 6 | 7 | You can install the package via composer: 8 | 9 | ```bash 10 | composer require beyondcode/fathom-api 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```php 16 | $analytics = new BeyondCode\FathomAnalytics($email, $password); 17 | 18 | $sites = $analytics->getSites(); 19 | 20 | $analytics->getCurrentVisitors($siteId); 21 | 22 | // Returns the visitor data for today 23 | $analytics->getData($siteId); 24 | 25 | // Returns the visitor data for the whole week until today 26 | $analytics->getData($siteId, Carbon::now()->startOfWeek()); 27 | 28 | // Returns the visitor data for two days ago until yesterday. 29 | $analytics->getData($siteId, Carbon::now()->subDays(2), Carbon::now()->subDays(1)); 30 | ``` 31 | 32 | ### Changelog 33 | 34 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 35 | 36 | ## Contributing 37 | 38 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 39 | 40 | ### Security 41 | 42 | If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker. 43 | 44 | ## Credits 45 | 46 | - [Marcel Pociot](https://github.com/mpociot) 47 | - [All Contributors](../../contributors) 48 | 49 | ## License 50 | 51 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 52 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "beyondcode/fathom-api", 3 | "description": "Fathom Analytics API", 4 | "keywords": [ 5 | "beyondcode", 6 | "fathom-api" 7 | ], 8 | "homepage": "https://github.com/beyondcode/fathom-api", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Marcel Pociot", 13 | "email": "marcel@beyondco.de", 14 | "homepage": "https://beyondcode.de", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^7.3", 20 | "nesbot/carbon": "^2.31", 21 | "symfony/browser-kit": "^5.1", 22 | "symfony/css-selector": "^5.2", 23 | "symfony/http-client": "^5.1", 24 | "symfony/mime": "^5.2" 25 | }, 26 | "require-dev": { 27 | "phpunit/phpunit": "^9.0" 28 | }, 29 | "autoload": { 30 | "psr-4": { 31 | "BeyondCode\\FathomAnalytics\\": "src" 32 | } 33 | }, 34 | "autoload-dev": { 35 | "psr-4": { 36 | "BeyondCode\\FathomAnalytics\\Tests\\": "tests" 37 | } 38 | }, 39 | "scripts": { 40 | "test": "vendor/bin/phpunit", 41 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 42 | 43 | }, 44 | "config": { 45 | "sort-packages": true 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/FathomAnalytics.php: -------------------------------------------------------------------------------- 1 | browser = new HttpBrowser(HttpClient::create()); 28 | 29 | $this->login($email, $password); 30 | } 31 | 32 | protected function login($email, $password, $oneTimePassword = null) 33 | { 34 | $this->browser->request('GET', static::LOGIN_URL); 35 | 36 | $this->browser->submitForm('Login', [ 37 | 'email' => $email, 38 | 'password' => $password 39 | ]); 40 | 41 | if($this->browser->getHistory()->current()->getUri() === static::TWO_FACTOR_AUTH_URL) { 42 | $this->browser->submitForm('Login', [ 43 | 'one_time_password' => $oneTimePassword, 44 | ]); 45 | } 46 | } 47 | 48 | public function getSites() 49 | { 50 | $result = ''; 51 | $this->browser->getCrawler()->filter('script')->each(function (Crawler $element) use (&$result) { 52 | $scriptContent = $element->text(); 53 | if (strpos($scriptContent, 'window.data') === 0) { 54 | preg_match('/window\.data = (.*?);/m', $scriptContent, $matches); 55 | $result = json_decode($matches[1]); 56 | } 57 | }); 58 | 59 | return $result->sites; 60 | } 61 | 62 | public function getCurrentVisitors($siteId) 63 | { 64 | $this->browser->request('GET', "https://app.usefathom.com/sites/{$siteId}/current_visitors"); 65 | 66 | return json_decode($this->browser->getResponse()->getContent()); 67 | } 68 | 69 | public function getData($siteId, Carbon $from = null, Carbon $to = null) 70 | { 71 | $from = $from ?? Carbon::today()->startOfDay(); 72 | $to = $to ?? Carbon::today()->endOfDay(); 73 | 74 | $this->browser->request('GET', "https://app.usefathom.com/sites/{$siteId}/data?from={$from->toDateTimeString()}&to={$to->toDateTimeString()}&site={$siteId}&range=today&tz=Europe%2FBerlin"); 75 | return json_decode($this->browser->getResponse()->getContent()); 76 | } 77 | } 78 | --------------------------------------------------------------------------------