Refactor NPS tracking data fetching to use API call
[MAILPOET-1815]
This commit is contained in:
committed by
M. Shull
parent
7bc16e4ba7
commit
e3741def1d
16
assets/js/src/analytics.js
Normal file
16
assets/js/src/analytics.js
Normal file
@ -0,0 +1,16 @@
|
||||
import MailPoet from 'mailpoet';
|
||||
|
||||
let trackingDataLoading = null;
|
||||
|
||||
function getTrackingData() {
|
||||
if (!trackingDataLoading) {
|
||||
trackingDataLoading = MailPoet.Ajax.post({
|
||||
api_version: window.mailpoet_api_version,
|
||||
endpoint: 'analytics',
|
||||
action: 'getTrackingData',
|
||||
});
|
||||
}
|
||||
return trackingDataLoading;
|
||||
}
|
||||
|
||||
export default getTrackingData;
|
@ -2,6 +2,7 @@ import React from 'react';
|
||||
import MailPoet from 'mailpoet';
|
||||
import ReactDOMServer from 'react-dom/server';
|
||||
import ReviewRequest from 'review_request.jsx';
|
||||
import getTrackingData from 'analytics.js';
|
||||
|
||||
const withNpsPoll = function withNpsPoll(Component) {
|
||||
return class extends React.Component {
|
||||
@ -13,6 +14,7 @@ const withNpsPoll = function withNpsPoll(Component) {
|
||||
};
|
||||
this.displayPoll = this.displayPoll.bind(this);
|
||||
this.showReviewRequestModal = this.showReviewRequestModal.bind(this);
|
||||
this.callSatismeter = this.callSatismeter.bind(this);
|
||||
}
|
||||
|
||||
showReviewRequestModal() {
|
||||
@ -41,16 +43,17 @@ const withNpsPoll = function withNpsPoll(Component) {
|
||||
displayPoll() {
|
||||
if (
|
||||
!this.state.pollShown
|
||||
&& (window.mailpoet_display_nps_poll)
|
||||
&& window.satismeter
|
||||
&& window.mailpoet_installed_at_isoFormat
|
||||
&& window.mailpoet_analytics_tracking_data
|
||||
) {
|
||||
this.setState({ pollShown: true });
|
||||
getTrackingData().then(this.callSatismeter);
|
||||
}
|
||||
}
|
||||
|
||||
callSatismeter(trackingData) {
|
||||
const newUsersPollId = '6L479eVPXk7pBn6S';
|
||||
const oldUsersPollId = 'k0aJAsQAWI2ERyGv';
|
||||
window.satismeter({
|
||||
forceSurvey: true,
|
||||
writeKey: window.mailpoet_is_new_user ? newUsersPollId : oldUsersPollId,
|
||||
userId: window.mailpoet_current_wp_user.ID + window.mailpoet_site_url,
|
||||
traits: {
|
||||
@ -59,14 +62,14 @@ const withNpsPoll = function withNpsPoll(Component) {
|
||||
createdAt: window.mailpoet_installed_at_isoFormat,
|
||||
mailpoetVersion: window.mailpoet_version,
|
||||
mailpoetPremiumIsActive: window.mailpoet_premium_active,
|
||||
newslettersSent: window.mailpoet_analytics_tracking_data.newslettersSent,
|
||||
welcomeEmails: window.mailpoet_analytics_tracking_data.welcomeEmails,
|
||||
postnotificationEmails: window.mailpoet_analytics_tracking_data.postnotificationEmails,
|
||||
woocommerceEmails: window.mailpoet_analytics_tracking_data.woocommerceEmails,
|
||||
subscribers: window.mailpoet_analytics_tracking_data.subscribers,
|
||||
lists: window.mailpoet_analytics_tracking_data.lists,
|
||||
sendingMethod: window.mailpoet_analytics_tracking_data.sendingMethod,
|
||||
woocommerceIsInstalled: window.mailpoet_analytics_tracking_data.woocommerceIsInstalled,
|
||||
newslettersSent: trackingData.newslettersSent,
|
||||
welcomeEmails: trackingData.welcomeEmails,
|
||||
postnotificationEmails: trackingData.postnotificationEmails,
|
||||
woocommerceEmails: trackingData.woocommerceEmails,
|
||||
subscribers: trackingData.subscribers,
|
||||
lists: trackingData.lists,
|
||||
sendingMethod: trackingData.sendingMethod,
|
||||
woocommerceIsInstalled: trackingData.woocommerceIsInstalled,
|
||||
},
|
||||
events: {
|
||||
submit: (response) => {
|
||||
@ -77,9 +80,11 @@ const withNpsPoll = function withNpsPoll(Component) {
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
if (!window.mailpoet_display_nps_poll) {
|
||||
return null;
|
||||
}
|
||||
if (window.satismeter) {
|
||||
this.displayPoll();
|
||||
} else {
|
||||
|
27
lib/API/JSON/v1/Analytics.php
Normal file
27
lib/API/JSON/v1/Analytics.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\API\JSON\v1;
|
||||
|
||||
use MailPoet\Analytics\Reporter;
|
||||
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
||||
use MailPoet\Config\AccessControl;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class Analytics extends APIEndpoint {
|
||||
|
||||
/** @var Reporter */
|
||||
private $reporter;
|
||||
|
||||
public $permissions = array(
|
||||
'global' => AccessControl::NO_ACCESS_RESTRICTION
|
||||
);
|
||||
|
||||
function __construct(Reporter $reporter) {
|
||||
$this->reporter = $reporter;
|
||||
}
|
||||
|
||||
function getTrackingData() {
|
||||
return $this->successResponse($this->reporter->getTrackingData());
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace MailPoet\API\JSON\v1;
|
||||
|
||||
use MailPoet\Analytics\Analytics;
|
||||
use MailPoet\Analytics\Analytics as AnalyticsHelper;
|
||||
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
||||
use MailPoet\API\JSON\Error as APIError;
|
||||
use MailPoet\Config\AccessControl;
|
||||
@ -56,7 +56,7 @@ class Services extends APIEndpoint {
|
||||
}
|
||||
|
||||
if (!empty($result['data']['public_id'])) {
|
||||
Analytics::setPublicId($result['data']['public_id']);
|
||||
AnalyticsHelper::setPublicId($result['data']['public_id']);
|
||||
}
|
||||
|
||||
if ($success_message) {
|
||||
@ -118,7 +118,7 @@ class Services extends APIEndpoint {
|
||||
}
|
||||
|
||||
if (!empty($result['data']['public_id'])) {
|
||||
Analytics::setPublicId($result['data']['public_id']);
|
||||
AnalyticsHelper::setPublicId($result['data']['public_id']);
|
||||
}
|
||||
|
||||
if ($success_message) {
|
||||
|
@ -54,8 +54,6 @@ class Menu {
|
||||
private $wp;
|
||||
/** @var ServicesChecker */
|
||||
private $servicesChecker;
|
||||
/** @var Reporter */
|
||||
private $analytics_reporter;
|
||||
|
||||
private $subscribers_over_limit;
|
||||
|
||||
@ -65,8 +63,7 @@ class Menu {
|
||||
SettingsController $settings,
|
||||
WPFunctions $wp,
|
||||
WooCommerceHelper $woocommerce_helper,
|
||||
ServicesChecker $servicesChecker,
|
||||
Reporter $analytics_reporter
|
||||
ServicesChecker $servicesChecker
|
||||
) {
|
||||
$this->renderer = $renderer;
|
||||
$this->access_control = $access_control;
|
||||
@ -74,7 +71,6 @@ class Menu {
|
||||
$this->settings = $settings;
|
||||
$this->woocommerce_helper = $woocommerce_helper;
|
||||
$this->servicesChecker = $servicesChecker;
|
||||
$this->analytics_reporter = $analytics_reporter;
|
||||
}
|
||||
|
||||
function init() {
|
||||
@ -692,7 +688,6 @@ class Menu {
|
||||
);
|
||||
|
||||
$data['is_new_user'] = $this->isNewUser();
|
||||
$data['analytics_tracking_data'] = $this->analytics_reporter->getTrackingData();
|
||||
|
||||
WPFunctions::get()->wpEnqueueScript('jquery-ui');
|
||||
WPFunctions::get()->wpEnqueueScript('jquery-ui-datepicker');
|
||||
|
@ -36,6 +36,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
->setAutowired(true)
|
||||
->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\MP\v1\API::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\Analytics::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\AutomatedLatestContent::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\CustomFields::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\Forms::class)->setPublic(true);
|
||||
|
@ -31,7 +31,6 @@
|
||||
var mailpoet_congratulations_loading_image = '<%= cdn_url('newsletter/congratulation-page-illustration-transparent-LQ.20181121-1440.png') %>';
|
||||
var mailpoet_main_page = '<%= mailpoet_main_page %>';
|
||||
var mailpoet_review_request_illustration_url = '<%= cdn_url('review-request/review-request-illustration.20181207-1345.png') %>';
|
||||
var mailpoet_analytics_tracking_data = <%= json_encode(analytics_tracking_data) %>;
|
||||
<% set newUser = (is_new_user == true) ? 'true' : 'false' %>
|
||||
var mailpoet_is_new_user = <%= newUser %>;
|
||||
</script>
|
||||
|
Reference in New Issue
Block a user