Skip to content

Commit

Permalink
chore: readme updated & unnecessary tracking data fetch removed
Browse files Browse the repository at this point in the history
  • Loading branch information
shuvomohajan committed Jun 1, 2024
1 parent fd352a3 commit efa19cf
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 48 deletions.
107 changes: 65 additions & 42 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ composer require bitapps/wp-telemetry
Initialize the telemetry client in your plugin's bootstrap file.

```php
function initialize_telemetry_client() {
function initialize_telemetry_client()
{
TelemetryConfig::setSlug($title);
TelemetryConfig::setTitle($slug);
TelemetryConfig::setPrefix($prefix);
TelemetryConfig::setVersion($version);
TelemetryConfig::setServerBaseUrl( 'https://api.example.com/' );

TelemetryConfig::setTermsUrl( 'https://example.com/terms' ); // optional
TelemetryConfig::setPolicyUrl( 'https://example.com/privacy' ); // optional
TelemetryConfig::setServerBaseUrl( 'https://api.example.com/' );
TelemetryConfig::setTermsUrl( 'https://example.com/terms/' ); // (optional)
TelemetryConfig::setPolicyUrl( 'https://example.com/privacy/' ); // (optional)

// initialize tracking and reporting
Telemetry::report()->init();
Expand All @@ -37,80 +38,102 @@ function initialize_telemetry_client() {
initialize_telemetry_client();
```

You are good to go! The telemetry client will start sending data to the default server.
**You are good to go! 👍️**

The telemetry client will start sending data to your configured api base url.

## Configuration

All the configuration should be done in the `initialize_telemetry_client()` function.
### # Activate/Deactivate Telemetry Tracking

### # Tracking Report Modify
You can add a setting in your plugin settings page to allow users to opt-in or opt-out of telemetry tracking. You can use the following methods to change the opt-in/opt-out status.

Add plugin information in tracking data
**⚡️ Opt In :**

```php
TelemetryConfig::report()
->addPluginData()
->init();
Telemetry::report()->trackingOptIn();
```

**Filter Hook to Add Additional Data :**
**⚡️ Opt Out :**

```php
Telemetry::report()->trackingOptOut();
```

### # Tracking Report Modify

**⚡️ Filter Hook to Add Additional Data :**

This filter allows adding additional data to track information used by the plugin. You can modify the `additional_data` array to include any custom data needed.

```php
$plugin_prefix = 'my_plugin_prefix_';
apply_filters($plugin_prefix . 'telemetry_additional_data', $additional_data);
```

add_filter($plugin_prefix . 'telemetry_additional_data', function($additional_data) {
**Usage**

// example: add custom data
$additional_data['my_custom_data'] = 'My Custom Data';
```php
add_filter($plugin_prefix . 'telemetry_additional_data', 'customize_additional_data', 10, 1);

function customize_additional_data($additional_data)
{
// Do your stuffs here
return $additional_data;
});
}
```

**Filter Hook To Modify Telemetry Data :**
**⚡️ Filter Hook To Modify Telemetry Data :**

This filter allows modification of the telemetry data array before it is sent.

```php
$plugin_prefix = 'my_plugin_prefix_';

add_filter($plugin_prefix . 'telemetry_data', function($telemetry_data) {
apply_filters($plugin_prefix . 'telemetry_data', $telemetry_data);
```

// example: remove some data
unset($telemetry_data['some_data']);
**Usage**

// example: add custom data
$telemetry_data['my_custom_data'] = 'My Custom Data';
```php
add_filter($plugin_prefix . 'telemetry_data', 'customize_telemetry_data', 10, 1);

function customize_telemetry_data($telemetry_data)
{
// Do your stuffs here
return $telemetry_data;
});
}
```

### # Deactivate Feedback Config
**⚡️ Add plugin information in tracking data**

You can customize the feedback survey by adding questions using `add_filter()`
```php
TelemetryConfig::report()
->addPluginData()
->init();
```

### # Deactivation Feedback Survey

**⚡️ Filter Hook to Add Deactivate Reasons :**

- **title** - The title of the question
- **placeholder** - The input placeholder of the question (optional)
This filter allows adding additional deactivate reasons to the feedback survey. You can modify the `deactivate_reasons` array to include any custom reasons needed.

```php
$prefix = 'my_plugin_prefix_';
apply_filters($plugin_prefix . 'deactivate_reasons', $deactivate_reasons);
```

add_filter($prefix . 'deactivate_reasons', function ($default_reasons) {
**Usage**

$default_reasons['my_custom_reason'] = [
'title' => 'My Custom Reason',
'placeholder' => 'Please specify the reason',
]
```php

$default_reasons['my_custom_reason_2'] = [
'title' => 'My Custom Reason 2',
'placeholder' => '',
]
add_filter($plugin_prefix . 'deactivate_reasons', 'add_deactivate_reasons', 10, 1);

return $default_reasons;
});
function add_deactivate_reasons($deactivate_reasons)
{
// example of adding a custom deactivate reason
$deactivate_reasons[] = [
'title' => 'What could we have done to improve your experience?',
'placeholder' => 'Please provide your feedback here',
];

return $deactivate_reasons;
}
```
15 changes: 9 additions & 6 deletions src/Telemetry/Report/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,19 @@ public function handleTrackingOptInOptOut()
return;
}

if (isset($_GET[TelemetryConfig::getPrefix() . 'tracking_opt_in']) && $_GET[TelemetryConfig::getPrefix() . 'tracking_opt_in'] === 'true') {
$optInKey = TelemetryConfig::getPrefix() . 'tracking_opt_in';

if (isset($_GET[$optInKey]) && $_GET[$optInKey] === 'true') {
$this->trackingOptIn();
wp_safe_redirect(remove_query_arg(TelemetryConfig::getPrefix() . 'tracking_opt_in'));
wp_safe_redirect(remove_query_arg($optInKey));
exit;
}

if (isset($_GET[TelemetryConfig::getPrefix() . 'tracking_opt_out'], $_GET[TelemetryConfig::getPrefix() . 'tracking_opt_out']) && $_GET[TelemetryConfig::getPrefix() . 'tracking_opt_out'] === 'true') {
$optOutKey = TelemetryConfig::getPrefix() . 'tracking_opt_out';

if (isset($_GET[$optOutKey]) && $_GET[$optOutKey] === 'true') {
$this->trackingOptOut();
wp_safe_redirect(remove_query_arg(TelemetryConfig::getPrefix() . 'tracking_opt_out'));
wp_safe_redirect(remove_query_arg($optOutKey));
exit;
}
}
Expand All @@ -105,7 +109,7 @@ public function trackingOptIn()
$this->scheduleEvent();
$this->sendTrackingReport();

do_action(TelemetryConfig::getPrefix() . 'tracking_opt_in', $this->getTrackingData());
do_action(TelemetryConfig::getPrefix() . 'tracking_opt_in');
}

public function trackingOptOut()
Expand All @@ -114,7 +118,6 @@ public function trackingOptOut()
update_option(TelemetryConfig::getPrefix() . 'tracking_notice_dismissed', true);

$this->trackingSkippedRequest();

$this->clearScheduleEvent();

do_action(TelemetryConfig::getPrefix() . 'tracking_opt_out');
Expand Down

0 comments on commit efa19cf

Please sign in to comment.