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,45 +43,48 @@ 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 });
|
||||
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: {
|
||||
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();
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
getTrackingData().then(this.callSatismeter);
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
if (!window.mailpoet_display_nps_poll) {
|
||||
return null;
|
||||
}
|
||||
if (window.satismeter) {
|
||||
this.displayPoll();
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user