Skip to content

Commit

Permalink
Feature: Add telemetry for the Event block (#7341)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjohnson authored Apr 29, 2024
1 parent aa9572d commit 07afc8d
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Tracking/Helpers/FormBlocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Give\Tracking\Helpers;

use Give\DonationForms\Models\DonationForm;

/**
* @unreleased
*/
class FormBlocks
{
/**
* @var DonationForm
* @unreleased
*/
protected $form;

/**
* @unreleased
*/
public function __construct(DonationForm $form)
{
$this->form = $form;
}

/**
* @unreleased
*/
public static function formId($formId): self
{
return new self(DonationForm::find($formId));
}

/**
* @unreleased
*/
public function hasBlock(string $blockName): bool
{
return (bool) $this->form->blocks->findByName($blockName);
}
}
21 changes: 21 additions & 0 deletions src/Tracking/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Anonymous Usage Tracking

When a site is opted in to anonymous usage tracking, GiveWP uses various hooks to collect data about how the plugin and related donation forms are configured.

Each grouping of usage data is collected on a corresponding hook in the WordPress request cycle.

During [shutdown](https://developer.wordpress.org/reference/hooks/shutdown/) any registered changes are offloaded to a background task using the [WordPress cron](https://developer.wordpress.org/plugins/cron/) system.

## Sending new tracking data

The data that is collected should belong to one of a few groups:

1. Environment configuration data
2. Plugin configuration data
3. Donation Form configuration data

Each of the groups has one or more related events with a corresponding hook and `TrackingData` class, which can be referenced in [TrackingServiceProvider](TrackingServiceProvider.php).

To add new tracking data, identify the appropriate event and the related `TrackingData`.

The returned array of data will be sent to the appropriate endpoint according the the [EventType](Enum/EventType.php) of the event.
3 changes: 3 additions & 0 deletions src/Tracking/TrackingData/DonationFormsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Give\Helpers\Form\Utils;
use Give\Tracking\Contracts\TrackData;
use Give\Tracking\Helpers\DonationStatuses;
use Give\Tracking\Helpers\FormBlocks;
use Give\Tracking\Repositories\TrackEvents;

/**
Expand Down Expand Up @@ -60,6 +61,7 @@ public function get()
/**
* Get forms data.
*
* @unreleased Add check for event block.
* @since 3.0.0 Add support for v3 forms
* @since 2.10.0
* @return array
Expand All @@ -81,6 +83,7 @@ protected function getData()
'form_template' => $this->getFormTemplate($formId),
'donor_count' => $this->formDonorCounts[$formId],
'revenue' => $this->formRevenues[$formId],
'event_block' => (int) FormBlocks::formId($formId)->hasBlock('givewp/event-tickets'),
];
$this->addAddonsInformation($temp, $formId);
$data[] = $temp;
Expand Down
7 changes: 7 additions & 0 deletions src/Tracking/TrackingData/GivePluginSettingsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Give\Tracking\TrackingData;

use Give\BetaFeatures\Facades\FeatureFlag;
use Give\Tracking\Contracts\TrackData;
use Give\Tracking\Repositories\Settings;

Expand Down Expand Up @@ -29,6 +30,7 @@ public function get()
/**
* Returns plugin global settings.
*
* @unreleased Add check for Event Tickets beta feature.
* @since 2.10.0
* @return array
*/
Expand Down Expand Up @@ -64,6 +66,11 @@ private function getGlobalSettings()

$data['active_payment_gateways'] = $this->getGatewaysLabels();

/**
* @unreleased Add check for Event Tickets beta feature.
*/
$data['is_event_tickets'] = FeatureFlag::eventTickets();

return $data;
}

Expand Down
44 changes: 44 additions & 0 deletions tests/Unit/Tracking/Helpers/FormBlocksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Give\Tests\Unit\Tracking\Helpers;

use Give\DonationForms\Models\DonationForm;
use Give\Donations\Models\Donation;
use Give\Donations\ValueObjects\DonationStatus;
use Give\Framework\Blocks\BlockCollection;
use Give\Framework\Blocks\BlockModel;
use Give\Framework\Database\DB;
use Give\Framework\Support\ValueObjects\Money;
use Give\Revenue\Listeners\UpdateRevenueWhenDonationAmountUpdated;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;
use Give\Tracking\Helpers\FormBlocks;

/**
* @since 2.20.1
*/
class FormBlocksTest extends TestCase
{
use RefreshDatabase;

public function testFormHasBlockByName()
{
$form = DonationForm::factory()->create();
$form->blocks->insertAfter('givewp/donor-name', new BlockModel('test/form-block-1'));
$form->save();

$this->assertTrue(
FormBlocks::formId($form->id)->hasBlock('test/form-block-1')
);
}

public function testFormDoesNotHaveBlockByName()
{
$form = DonationForm::factory()->create();

$this->assertFalse(
FormBlocks::formId($form->id)->hasBlock('test/form-block-1')
);
}
}

0 comments on commit 07afc8d

Please sign in to comment.