Anonymous Segment.com tracking. Closes #91.
This commit is contained in:
committed by
Joseph Cohen
parent
a63c65a6c3
commit
5c391cc888
@@ -50,7 +50,13 @@ class OneClickDeployCommand extends Command
|
||||
public function fire()
|
||||
{
|
||||
if ($this->migrate) {
|
||||
return $this->runMigrations();
|
||||
$migrations = $this->runMigrations();
|
||||
|
||||
segment_track('Installation', [
|
||||
'event' => 'Heroku Deployment',
|
||||
]);
|
||||
|
||||
return $migrations;
|
||||
}
|
||||
|
||||
$this->info('Please run "php artisan migrate" to finish the installation.');
|
||||
|
||||
@@ -109,6 +109,11 @@ class DashComponentController extends Controller
|
||||
$component->update($_component);
|
||||
|
||||
if (! $component->isValid()) {
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Edit Component',
|
||||
'success' => false,
|
||||
]);
|
||||
|
||||
return Redirect::back()->withInput(Binput::all())
|
||||
->with('title', sprintf(
|
||||
'<strong>%s</strong> %s',
|
||||
@@ -118,6 +123,11 @@ class DashComponentController extends Controller
|
||||
->with('errors', $component->getErrors());
|
||||
}
|
||||
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Edit Component',
|
||||
'success' => true,
|
||||
]);
|
||||
|
||||
// The component was added successfully, so now let's deal with the tags.
|
||||
$tags = preg_split('/ ?, ?/', $tags);
|
||||
|
||||
@@ -168,6 +178,11 @@ class DashComponentController extends Controller
|
||||
$component = Component::create($_component);
|
||||
|
||||
if (! $component->isValid()) {
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Created Component',
|
||||
'success' => false,
|
||||
]);
|
||||
|
||||
return Redirect::back()->withInput(Binput::all())
|
||||
->with('title', sprintf(
|
||||
'<strong>%s</strong> %s',
|
||||
@@ -177,6 +192,11 @@ class DashComponentController extends Controller
|
||||
->with('errors', $component->getErrors());
|
||||
}
|
||||
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Created Component',
|
||||
'success' => true,
|
||||
]);
|
||||
|
||||
// The component was added successfully, so now let's deal with the tags.
|
||||
$tags = preg_split('/ ?, ?/', $tags);
|
||||
|
||||
@@ -207,6 +227,10 @@ class DashComponentController extends Controller
|
||||
*/
|
||||
public function deleteComponentAction(Component $component)
|
||||
{
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Deleted Component',
|
||||
]);
|
||||
|
||||
$component->delete();
|
||||
|
||||
return Redirect::back();
|
||||
@@ -221,6 +245,10 @@ class DashComponentController extends Controller
|
||||
*/
|
||||
public function deleteComponentGroupAction(ComponentGroup $group)
|
||||
{
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Deleted Component Group',
|
||||
]);
|
||||
|
||||
$group->delete();
|
||||
|
||||
return Redirect::back();
|
||||
@@ -248,6 +276,11 @@ class DashComponentController extends Controller
|
||||
$group = ComponentGroup::create(Binput::get('group'));
|
||||
|
||||
if (! $group->isValid()) {
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Created Component Group',
|
||||
'success' => false,
|
||||
]);
|
||||
|
||||
return Redirect::back()->withInput(Binput::all())
|
||||
->with('title', sprintf(
|
||||
'<strong>%s</strong> %s',
|
||||
@@ -257,6 +290,11 @@ class DashComponentController extends Controller
|
||||
->with('errors', $group->getErrors());
|
||||
}
|
||||
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Created Component Group',
|
||||
'success' => true,
|
||||
]);
|
||||
|
||||
$successMsg = sprintf(
|
||||
'<strong>%s</strong> %s',
|
||||
trans('dashboard.notifications.awesome'),
|
||||
|
||||
@@ -66,6 +66,11 @@ class DashIncidentController extends Controller
|
||||
$incident = Incident::create($incidentData);
|
||||
|
||||
if (! $incident->isValid()) {
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Created Incident',
|
||||
'success' => false,
|
||||
]);
|
||||
|
||||
return Redirect::back()->withInput(Binput::all())
|
||||
->with('title', sprintf(
|
||||
'<strong>%s</strong> %s',
|
||||
@@ -82,6 +87,11 @@ class DashIncidentController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Created Incident',
|
||||
'success' => true,
|
||||
]);
|
||||
|
||||
$successMsg = sprintf(
|
||||
'<strong>%s</strong> %s',
|
||||
trans('dashboard.notifications.awesome'),
|
||||
@@ -127,6 +137,10 @@ class DashIncidentController extends Controller
|
||||
*/
|
||||
public function deleteTemplateAction(IncidentTemplate $template)
|
||||
{
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Deleted Incident Template',
|
||||
]);
|
||||
|
||||
$template->delete();
|
||||
|
||||
return Redirect::back();
|
||||
@@ -143,6 +157,11 @@ class DashIncidentController extends Controller
|
||||
$template = IncidentTemplate::create($_template);
|
||||
|
||||
if (! $template->isValid()) {
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Created Incident Template',
|
||||
'success' => false,
|
||||
]);
|
||||
|
||||
return Redirect::back()->withInput(Binput::all())
|
||||
->with('title', sprintf(
|
||||
'<strong>%s</strong> %s',
|
||||
@@ -152,6 +171,11 @@ class DashIncidentController extends Controller
|
||||
->with('errors', $template->getErrors());
|
||||
}
|
||||
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Created Incident Template',
|
||||
'success' => true,
|
||||
]);
|
||||
|
||||
$successMsg = sprintf(
|
||||
'<strong>%s</strong> %s',
|
||||
trans('dashboard.notifications.awesome'),
|
||||
@@ -170,6 +194,10 @@ class DashIncidentController extends Controller
|
||||
*/
|
||||
public function deleteIncidentAction(Incident $incident)
|
||||
{
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Deleted Incident',
|
||||
]);
|
||||
|
||||
$incident->delete();
|
||||
|
||||
return Redirect::back();
|
||||
@@ -204,6 +232,11 @@ class DashIncidentController extends Controller
|
||||
$incident->update($_incident);
|
||||
|
||||
if (! $incident->isValid()) {
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Edited Incident',
|
||||
'success' => false,
|
||||
]);
|
||||
|
||||
return Redirect::back()->withInput(Binput::all())
|
||||
->with('title', sprintf(
|
||||
'<strong>%s</strong> %s',
|
||||
@@ -213,6 +246,11 @@ class DashIncidentController extends Controller
|
||||
->with('errors', $incident->getErrors());
|
||||
}
|
||||
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Edited Incident',
|
||||
'success' => true,
|
||||
]);
|
||||
|
||||
$successMsg = sprintf(
|
||||
'<strong>%s</strong> %s',
|
||||
trans('dashboard.notifications.awesome'),
|
||||
@@ -231,6 +269,10 @@ class DashIncidentController extends Controller
|
||||
*/
|
||||
public function editTemplateAction(IncidentTemplate $template)
|
||||
{
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Edited Incident Template',
|
||||
]);
|
||||
|
||||
$template->update(Binput::get('template'));
|
||||
|
||||
return Redirect::back()->with('updatedTemplate', $template);
|
||||
|
||||
@@ -60,6 +60,11 @@ class DashTeamController extends Controller
|
||||
$user = User::create(Binput::all());
|
||||
|
||||
if (! $user->isValid()) {
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Added User',
|
||||
'success' => false,
|
||||
]);
|
||||
|
||||
return Redirect::back()->withInput(Binput::except('password'))
|
||||
->with('title', sprintf(
|
||||
'<strong>%s</strong> %s',
|
||||
@@ -69,6 +74,11 @@ class DashTeamController extends Controller
|
||||
->with('errors', $user->getErrors());
|
||||
}
|
||||
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Added User',
|
||||
'success' => true,
|
||||
]);
|
||||
|
||||
$successMsg = sprintf(
|
||||
'<strong>%s</strong> %s',
|
||||
trans('dashboard.notifications.awesome'),
|
||||
@@ -98,6 +108,11 @@ class DashTeamController extends Controller
|
||||
$user->update($items);
|
||||
|
||||
if (! $user->isValid()) {
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Updated User',
|
||||
'success' => false,
|
||||
]);
|
||||
|
||||
return Redirect::back()->withInput(Binput::except('password'))
|
||||
->with('title', sprintf(
|
||||
'<strong>%s</strong> %s',
|
||||
@@ -107,6 +122,11 @@ class DashTeamController extends Controller
|
||||
->with('errors', $user->getErrors());
|
||||
}
|
||||
|
||||
segment_track('Dashboard', [
|
||||
'event' => 'Updated User',
|
||||
'success' => true,
|
||||
]);
|
||||
|
||||
$successMsg = sprintf(
|
||||
'<strong>%s</strong> %s',
|
||||
trans('dashboard.notifications.awesome'),
|
||||
|
||||
@@ -39,8 +39,18 @@ class DashUserController extends Controller
|
||||
// Let's enable/disable auth
|
||||
if ($enable2FA && ! Auth::user()->hasTwoFactor) {
|
||||
$items['google_2fa_secret'] = Google2FA::generateSecretKey();
|
||||
|
||||
segment_track('User Management', [
|
||||
'event' => 'enabled_two_factor',
|
||||
'value' => true,
|
||||
]);
|
||||
} elseif (! $enable2FA) {
|
||||
$items['google_2fa_secret'] = '';
|
||||
|
||||
segment_track('User Management', [
|
||||
'event' => 'enabled_two_factor',
|
||||
'value' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
if (trim($passwordChange) === '') {
|
||||
@@ -76,6 +86,10 @@ class DashUserController extends Controller
|
||||
*/
|
||||
public function regenerateApiKey(User $user)
|
||||
{
|
||||
segment_track('User Management', [
|
||||
'event' => 'regenrated_api_token'
|
||||
]);
|
||||
|
||||
$user->api_key = User::generateApiKey();
|
||||
$user->save();
|
||||
|
||||
|
||||
@@ -22,6 +22,10 @@ class HomeController extends Controller
|
||||
*/
|
||||
public function showIndex()
|
||||
{
|
||||
segment_track('Status Page', [
|
||||
'event' => 'Landed',
|
||||
]);
|
||||
|
||||
$components = Component::orderBy('order')->orderBy('created_at')->get();
|
||||
|
||||
$allIncidents = [];
|
||||
@@ -38,6 +42,21 @@ class HomeController extends Controller
|
||||
try {
|
||||
// If date provided is valid
|
||||
$oldDate = Date::createFromFormat('Y-m-d', Binput::get('start_date'));
|
||||
|
||||
segment_track('Status Page', [
|
||||
'start_date' => $oldDate->format('Y-m-d'),
|
||||
]);
|
||||
|
||||
if (Setting::get('app_tracking')) {
|
||||
Segment::track([
|
||||
'userId' => Config::get('app.key'),
|
||||
'event' => 'Home Page',
|
||||
'properties' => [
|
||||
'start_date' => $oldDate,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
// If trying to get a future date fallback to today
|
||||
if ($today->gt($oldDate)) {
|
||||
$startDate = $oldDate;
|
||||
|
||||
@@ -47,6 +47,10 @@ class SetupController extends Controller
|
||||
{
|
||||
$postData = Binput::all();
|
||||
|
||||
segment_track('Setup', [
|
||||
'step' => '1',
|
||||
]);
|
||||
|
||||
$v = Validator::make($postData, [
|
||||
'settings.app_name' => 'required',
|
||||
'settings.app_domain' => 'required',
|
||||
@@ -72,6 +76,10 @@ class SetupController extends Controller
|
||||
{
|
||||
$postData = Binput::all();
|
||||
|
||||
segment_track('Setup', [
|
||||
'step' => '2',
|
||||
]);
|
||||
|
||||
$v = Validator::make($postData, [
|
||||
'settings.app_name' => 'required',
|
||||
'settings.app_domain' => 'required',
|
||||
|
||||
@@ -25,6 +25,10 @@ class LoadConfigServiceProvider extends ServiceProvider
|
||||
// Don't throw any errors, we may not be setup yet.
|
||||
}
|
||||
|
||||
// Set the Segment.com settings.
|
||||
$segmentRepository = $this->app->make('CachetHQ\Cachet\Segment\RepositoryInterface');
|
||||
$this->app->config->set('cachethq/segment::write_key', $segmentRepository->fetch());
|
||||
|
||||
// Override default app values.
|
||||
$this->app->config->set('app.url', $appDomain ?: $this->app->config->get('app.url'));
|
||||
$this->app->config->set('app.locale', $appLocale ?: $this->app->config->get('app.locale'));
|
||||
|
||||
37
src/Providers/SegmentApiServiceProvider.php
Normal file
37
src/Providers/SegmentApiServiceProvider.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Providers;
|
||||
|
||||
use CachetHQ\Cachet\Segment\CacheRepository;
|
||||
use CachetHQ\Cachet\Segment\HttpRepository;
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class SegmentApiServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Boot the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->singleton('CachetHQ\Cachet\Segment\RepositoryInterface', function () {
|
||||
$url = 'https://gist.githubusercontent.com/jbrooksuk/5de24bc1cf90fb1a3d57/raw/cachet.json';
|
||||
$guzzleClient = new Client();
|
||||
$client = new HttpRepository($guzzleClient, $url);
|
||||
|
||||
return new CacheRepository($client);
|
||||
});
|
||||
}
|
||||
}
|
||||
55
src/Segment/CacheRepository.php
Normal file
55
src/Segment/CacheRepository.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Segment;
|
||||
|
||||
use CachetHQ\Cachet\Facades\Setting;
|
||||
use CachetHQ\Cachet\Models\Setting as SettingModel;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class CacheRepository implements RepositoryInterface
|
||||
{
|
||||
/**
|
||||
* @var \CachetHQ\Cachet\Segment\HttpRepository
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* Instantiates a new instance of the Cache Repository.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Segment\HttpRepository $repository
|
||||
*/
|
||||
public function __construct(RepositoryInterface $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether to use the segment_write_key setting or to fetch a new.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function fetch()
|
||||
{
|
||||
// Firstly, does the setting exist?
|
||||
if (false === ($writeKey = Setting::get('segment_write_key'))) {
|
||||
// No, let's go fetch it.
|
||||
$writeKey = $this->repository->fetch();
|
||||
Setting::set('segment_write_key', $writeKey);
|
||||
} else {
|
||||
// It does, but how old is it?
|
||||
$setting = SettingModel::where('name', 'segment_write_key')->first();
|
||||
|
||||
// It's older than an hour, let's refresh
|
||||
if ($setting->updated_at->lt(Carbon::now()->subHour())) {
|
||||
$writeKey = $this->repository->fetch();
|
||||
|
||||
// Update the setting. This is done manual to make sure updated_at is overwritten.
|
||||
$setting->value = $writeKey;
|
||||
$setting->updated_at = Carbon::now();
|
||||
$setting->save();
|
||||
}
|
||||
}
|
||||
|
||||
return $writeKey;
|
||||
}
|
||||
}
|
||||
40
src/Segment/HttpRepository.php
Normal file
40
src/Segment/HttpRepository.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Segment;
|
||||
|
||||
use GuzzleHttp\ClientInterface;
|
||||
|
||||
class HttpRepository implements RepositoryInterface
|
||||
{
|
||||
/**
|
||||
* @var \Guzzle\GuzzleClient
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $url;
|
||||
|
||||
/**
|
||||
* Instantiates a new instance of the SegmentApi class.
|
||||
*
|
||||
* @param \Guzzle\GuzzleClient $client
|
||||
* @param string $url
|
||||
*/
|
||||
public function __construct(ClientInterface $client, $url)
|
||||
{
|
||||
$this->client = $client;
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the segment_write_key from the given url.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function fetch()
|
||||
{
|
||||
return $this->client->get($this->url)->json()['segment_write_key'];
|
||||
}
|
||||
}
|
||||
13
src/Segment/RepositoryInterface.php
Normal file
13
src/Segment/RepositoryInterface.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Segment;
|
||||
|
||||
interface RepositoryInterface
|
||||
{
|
||||
/**
|
||||
* Returns the segment_write_key.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function fetch();
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use CachetHQ\Cachet\Facades\Setting;
|
||||
use CachetHQ\Segment\Facades\Segment;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Request;
|
||||
|
||||
@@ -78,3 +80,77 @@ if (!function_exists('set_active')) {
|
||||
return empty($classes) ? '' : "class=\"{$class}\"";
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('segment_identify')) {
|
||||
/**
|
||||
* Identifies the user for Segment.com.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function segment_identify()
|
||||
{
|
||||
if (Setting::get('app_track')) {
|
||||
return Segment::identify([
|
||||
'anonymousId' => Config::get('app.key'),
|
||||
'context' => [
|
||||
'locale' => Config::get('app.locale'),
|
||||
'timezone' => Setting::get('app_timezone'),
|
||||
],
|
||||
]);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('segment_track')) {
|
||||
/**
|
||||
* Tracks events in Segment.com.
|
||||
*
|
||||
* @param string $event
|
||||
* @param array $properties
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function segment_track($event, array $properties)
|
||||
{
|
||||
if (Setting::get('app_track')) {
|
||||
return Segment::track([
|
||||
'anonymousId' => Config::get('app.key'),
|
||||
'event' => $event,
|
||||
'properties' => $properties,
|
||||
'context' => [
|
||||
'locale' => Config::get('app.locale'),
|
||||
'timezone' => Setting::get('app_timezone'),
|
||||
],
|
||||
]);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('segment_page')) {
|
||||
/**
|
||||
* Tracks pages in Segment.com.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function segment_page($page)
|
||||
{
|
||||
if (Setting::get('app_track')) {
|
||||
return Segment::page([
|
||||
'anonymousId' => Config::get('app.key'),
|
||||
'page' => $page,
|
||||
'context' => [
|
||||
'locale' => Config::get('app.locale'),
|
||||
'timezone' => Setting::get('app_timezone'),
|
||||
],
|
||||
]);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user