Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add campaign tracking data #7757

Open
wants to merge 2 commits into
base: epic/campaigns
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Tracking/Enum/EventType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ final class EventType extends Enum
const DONATION_METRICS = 'donation-metrics';
const DONATION_FORM_UPDATED = 'donation-form-updated';
const SITE_UPDATED = 'site-updated';
const CAMPAIGN_UPDATED = 'campaign-updated';
}
30 changes: 30 additions & 0 deletions src/Tracking/Events/CampaignsTracking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Give\Tracking\Events;

use Give\Tracking\Contracts\TrackEvent;
use Give\Tracking\Enum\EventType;
use Give\Tracking\TrackingData\CampaignsData;
use Give\Tracking\TrackingData\DonationFormsData;
use Give\Tracking\TrackRegisterer;

/**
* @unreleased
*/
class CampaignsTracking extends TrackEvent
{
/**
* @unreleased
* @var string
*/
protected $dataClassName = CampaignsData::class;

/**
* @unreleased
*/
public function __construct(TrackRegisterer $track)
{
$this->eventType = new EventType(EventType::CAMPAIGN_UPDATED);
parent::__construct($track);
}
}
48 changes: 48 additions & 0 deletions src/Tracking/TrackingData/CampaignsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Give\Tracking\TrackingData;

use Give\Campaigns\Models\Campaign;
use Give\Framework\Database\DB;
use Give\Tracking\Contracts\TrackData;
use Give\Tracking\Repositories\TrackEvents;

/**
* @unreleased
*/
class CampaignsData implements TrackData
{
/**
* @var TrackEvents
*/
protected $trackEvents;

/**
* DonationFormsData constructor.
*
* @param TrackEvents $trackEvents
*/
public function __construct(TrackEvents $trackEvents)
{
$this->trackEvents = $trackEvents;
}

/**
* @inheritdoc
*/
public function get()
{
$query = Campaign::query()
->select('count(forms.form_id) as formCount')
->leftJoin('give_campaign_forms', 'campaigns.id', 'forms.campaign_id', 'forms')
->groupBy('campaigns.id');

return array_map(function($campaign) {
return [
'campaign_id' => $campaign->id,
'campaign_status' => $campaign->status,
'campaign_form_count' => $campaign->formCount,
];
}, DB::get_results($query->getSQL()));
}
}
2 changes: 2 additions & 0 deletions src/Tracking/TrackingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Give\Helpers\Hooks;
use Give\ServiceProviders\ServiceProvider;
use Give\Tracking\Events\CampaignsTracking;
use Give\Tracking\Events\DonationFormsTracking;
use Give\Tracking\Events\DonationMetricsTracking;
use Give\Tracking\Events\EditedDonationFormsTracking;
Expand Down Expand Up @@ -114,5 +115,6 @@ private function registerTrackEvents()
Hooks::addAction('update_option_give_settings', GivePluginSettingsTracking::class, 'record');
Hooks::addAction('update_option_active_plugins', PluginsTracking::class, 'record');
Hooks::addAction('switch_theme', ThemeTracking::class, 'record');
Hooks::addAction('givewp_campaign_updated', CampaignsTracking::class, 'record');
}
}
Loading