Refactor NPS tracking data fetching to use API call

[MAILPOET-1815]
This commit is contained in:
Rostislav Wolny
2019-03-08 13:43:53 +01:00
committed by M. Shull
parent 7bc16e4ba7
commit e3741def1d
7 changed files with 85 additions and 42 deletions

View 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;

View File

@ -2,6 +2,7 @@ import React from 'react';
import MailPoet from 'mailpoet'; import MailPoet from 'mailpoet';
import ReactDOMServer from 'react-dom/server'; import ReactDOMServer from 'react-dom/server';
import ReviewRequest from 'review_request.jsx'; import ReviewRequest from 'review_request.jsx';
import getTrackingData from 'analytics.js';
const withNpsPoll = function withNpsPoll(Component) { const withNpsPoll = function withNpsPoll(Component) {
return class extends React.Component { return class extends React.Component {
@ -13,6 +14,7 @@ const withNpsPoll = function withNpsPoll(Component) {
}; };
this.displayPoll = this.displayPoll.bind(this); this.displayPoll = this.displayPoll.bind(this);
this.showReviewRequestModal = this.showReviewRequestModal.bind(this); this.showReviewRequestModal = this.showReviewRequestModal.bind(this);
this.callSatismeter = this.callSatismeter.bind(this);
} }
showReviewRequestModal() { showReviewRequestModal() {
@ -41,45 +43,48 @@ const withNpsPoll = function withNpsPoll(Component) {
displayPoll() { displayPoll() {
if ( if (
!this.state.pollShown !this.state.pollShown
&& (window.mailpoet_display_nps_poll)
&& window.satismeter
&& window.mailpoet_installed_at_isoFormat && window.mailpoet_installed_at_isoFormat
&& window.mailpoet_analytics_tracking_data
) { ) {
this.setState({ pollShown: true }); this.setState({ pollShown: true });
const newUsersPollId = '6L479eVPXk7pBn6S'; getTrackingData().then(this.callSatismeter);
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: {
name: window.mailpoet_current_wp_user.user_nicename,
email: window.mailpoet_current_wp_user.user_email,
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,
},
events: {
submit: (response) => {
if (response.rating >= 9 && response.completed) {
this.showReviewRequestModal();
}
},
},
});
} }
} }
callSatismeter(trackingData) {
const newUsersPollId = '6L479eVPXk7pBn6S';
const oldUsersPollId = 'k0aJAsQAWI2ERyGv';
window.satismeter({
writeKey: window.mailpoet_is_new_user ? newUsersPollId : oldUsersPollId,
userId: window.mailpoet_current_wp_user.ID + window.mailpoet_site_url,
traits: {
name: window.mailpoet_current_wp_user.user_nicename,
email: window.mailpoet_current_wp_user.user_email,
createdAt: window.mailpoet_installed_at_isoFormat,
mailpoetVersion: window.mailpoet_version,
mailpoetPremiumIsActive: window.mailpoet_premium_active,
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) => {
if (response.rating >= 9 && response.completed) {
this.showReviewRequestModal();
}
},
},
});
}
render() { render() {
if (!window.mailpoet_display_nps_poll) {
return null;
}
if (window.satismeter) { if (window.satismeter) {
this.displayPoll(); this.displayPoll();
} else { } else {

View 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());
}
}

View File

@ -2,7 +2,7 @@
namespace MailPoet\API\JSON\v1; 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\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError; use MailPoet\API\JSON\Error as APIError;
use MailPoet\Config\AccessControl; use MailPoet\Config\AccessControl;
@ -56,7 +56,7 @@ class Services extends APIEndpoint {
} }
if (!empty($result['data']['public_id'])) { if (!empty($result['data']['public_id'])) {
Analytics::setPublicId($result['data']['public_id']); AnalyticsHelper::setPublicId($result['data']['public_id']);
} }
if ($success_message) { if ($success_message) {
@ -118,7 +118,7 @@ class Services extends APIEndpoint {
} }
if (!empty($result['data']['public_id'])) { if (!empty($result['data']['public_id'])) {
Analytics::setPublicId($result['data']['public_id']); AnalyticsHelper::setPublicId($result['data']['public_id']);
} }
if ($success_message) { if ($success_message) {

View File

@ -54,8 +54,6 @@ class Menu {
private $wp; private $wp;
/** @var ServicesChecker */ /** @var ServicesChecker */
private $servicesChecker; private $servicesChecker;
/** @var Reporter */
private $analytics_reporter;
private $subscribers_over_limit; private $subscribers_over_limit;
@ -65,8 +63,7 @@ class Menu {
SettingsController $settings, SettingsController $settings,
WPFunctions $wp, WPFunctions $wp,
WooCommerceHelper $woocommerce_helper, WooCommerceHelper $woocommerce_helper,
ServicesChecker $servicesChecker, ServicesChecker $servicesChecker
Reporter $analytics_reporter
) { ) {
$this->renderer = $renderer; $this->renderer = $renderer;
$this->access_control = $access_control; $this->access_control = $access_control;
@ -74,7 +71,6 @@ class Menu {
$this->settings = $settings; $this->settings = $settings;
$this->woocommerce_helper = $woocommerce_helper; $this->woocommerce_helper = $woocommerce_helper;
$this->servicesChecker = $servicesChecker; $this->servicesChecker = $servicesChecker;
$this->analytics_reporter = $analytics_reporter;
} }
function init() { function init() {
@ -692,7 +688,6 @@ class Menu {
); );
$data['is_new_user'] = $this->isNewUser(); $data['is_new_user'] = $this->isNewUser();
$data['analytics_tracking_data'] = $this->analytics_reporter->getTrackingData();
WPFunctions::get()->wpEnqueueScript('jquery-ui'); WPFunctions::get()->wpEnqueueScript('jquery-ui');
WPFunctions::get()->wpEnqueueScript('jquery-ui-datepicker'); WPFunctions::get()->wpEnqueueScript('jquery-ui-datepicker');

View File

@ -36,6 +36,7 @@ class ContainerConfigurator implements IContainerConfigurator {
->setAutowired(true) ->setAutowired(true)
->setPublic(true); ->setPublic(true);
$container->autowire(\MailPoet\API\MP\v1\API::class)->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\AutomatedLatestContent::class)->setPublic(true);
$container->autowire(\MailPoet\API\JSON\v1\CustomFields::class)->setPublic(true); $container->autowire(\MailPoet\API\JSON\v1\CustomFields::class)->setPublic(true);
$container->autowire(\MailPoet\API\JSON\v1\Forms::class)->setPublic(true); $container->autowire(\MailPoet\API\JSON\v1\Forms::class)->setPublic(true);

View File

@ -31,7 +31,6 @@
var mailpoet_congratulations_loading_image = '<%= cdn_url('newsletter/congratulation-page-illustration-transparent-LQ.20181121-1440.png') %>'; 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_main_page = '<%= mailpoet_main_page %>';
var mailpoet_review_request_illustration_url = '<%= cdn_url('review-request/review-request-illustration.20181207-1345.png') %>'; 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' %> <% set newUser = (is_new_user == true) ? 'true' : 'false' %>
var mailpoet_is_new_user = <%= newUser %>; var mailpoet_is_new_user = <%= newUser %>;
</script> </script>