analytics : use a unique public id

This commit is contained in:
qfrery
2018-06-11 13:28:32 +01:00
committed by qfrery
parent e8b0e77d0e
commit 76686e08fe
6 changed files with 69 additions and 3 deletions

View File

@ -7,11 +7,22 @@ window.mixpanelTrackingId = "172e1ec7e7e6300e41defee3548dcf42";
if (mailpoet_analytics_enabled) { if (mailpoet_analytics_enabled) {
mixpanel.init(window.mixpanelTrackingId); mixpanel.init(window.mixpanelTrackingId, {
loaded: function(mixpanel) {
// used in lib/Analytics/Analytics.php
document.cookie = "mixpanel_distinct_id=" + mixpanel.get_distinct_id();
}
});
mixpanel.register({'Platform': 'Plugin'}); mixpanel.register({'Platform': 'Plugin'});
if(window.mailpoet_analytics_new_public_id === true) {
mixpanel.alias(window.mailpoet_analytics_public_id);
} else {
mixpanel.identify(window.mailpoet_analytics_public_id);
}
if (mailpoet_analytics_data != null) { if (mailpoet_analytics_data != null) {
//TODO mixpanel.identify(userId);
mixpanel.people.set(mailpoet_analytics_data); mixpanel.people.set(mailpoet_analytics_data);
} }

View File

@ -6,6 +6,7 @@ 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;
use MailPoet\Config\Installer; use MailPoet\Config\Installer;
use MailPoet\Models\Setting;
use MailPoet\Services\Bridge; use MailPoet\Services\Bridge;
use MailPoet\WP\DateTime; use MailPoet\WP\DateTime;
@ -53,6 +54,10 @@ class Services extends APIEndpoint {
); );
} }
if(!empty($result['data']['public_id'])) {
Analytics::setPublicId($result['data']['public_id']);
}
if($success_message) { if($success_message) {
return $this->successResponse(array('message' => $success_message)); return $this->successResponse(array('message' => $success_message));
} }
@ -111,6 +116,10 @@ class Services extends APIEndpoint {
); );
} }
if(!empty($result['data']['public_id'])) {
Analytics::setPublicId($result['data']['public_id']);
}
if($success_message) { if($success_message) {
return $this->successResponse( return $this->successResponse(
array('message' => $success_message), array('message' => $success_message),

View File

@ -34,6 +34,38 @@ class Analytics {
return !empty($analytics_settings['enabled']) === true; return !empty($analytics_settings['enabled']) === true;
} }
static function setPublicId($new_public_id) {
$current_public_id = Setting::getValue('public_id');
if($current_public_id !== $new_public_id) {
Setting::setValue('public_id', $new_public_id);
Setting::setValue('new_public_id', 'true');
}
}
/** @return string */
function getPublicId() {
$public_id = Setting::getValue('public_id');
// if we didn't get the user public_id from the shop yet : we create one based on mixpanel distinct_id
if(empty($public_id) && !empty($_COOKIE['mixpanel_distinct_id'])) {
// the public id has to be diffent that mixpanel_distinct_id in order to be used on different browser
$mixpanel_distinct_id = md5($_COOKIE['mixpanel_distinct_id']);
Setting::setValue('public_id', $mixpanel_distinct_id);
Setting::setValue('new_public_id', 'true');
return $mixpanel_distinct_id;
}
return $public_id;
}
/** @return boolean */
function isPublicIdNew() {
$new_public_id = Setting::getValue('new_public_id');
if($new_public_id === 'true') {
Setting::setValue('new_public_id', 'false');
return true;
}
return false;
}
private function shouldSend() { private function shouldSend() {
if(!$this->isEnabled()) { if(!$this->isEnabled()) {
return false; return false;

View File

@ -21,6 +21,16 @@ class Analytics extends \Twig_Extension {
array($analytics, 'isEnabled'), array($analytics, 'isEnabled'),
array('is_safe' => array('all')) array('is_safe' => array('all'))
), ),
new \Twig_SimpleFunction(
'get_analytics_public_id',
array($analytics, 'getPublicId'),
array('is_safe' => array('all'))
),
new \Twig_SimpleFunction(
'is_analytics_public_id_new',
array($analytics, 'isPublicIdNew'),
array('is_safe' => array('all'))
)
); );
} }
} }

View File

@ -70,6 +70,7 @@ class BridgeTest extends \MailPoetTest {
expect($result)->notEmpty(); expect($result)->notEmpty();
expect($result['state'])->equals(Bridge::KEY_EXPIRING); expect($result['state'])->equals(Bridge::KEY_EXPIRING);
expect($result['data']['expire_at'])->notEmpty(); expect($result['data']['expire_at'])->notEmpty();
expect($result['data']['public_id'])->notEmpty();
} }
function testItChecksAlreadyUsed() { function testItChecksAlreadyUsed() {
@ -147,6 +148,7 @@ class BridgeTest extends \MailPoetTest {
expect($result)->notEmpty(); expect($result)->notEmpty();
expect($result['state'])->equals(Bridge::KEY_EXPIRING); expect($result['state'])->equals(Bridge::KEY_EXPIRING);
expect($result['data']['expire_at'])->notEmpty(); expect($result['data']['expire_at'])->notEmpty();
expect($result['data']['public_id'])->notEmpty();
} }
function testItReturnsErrorStateOnEmptyAPIResponseCodeDuringPremiumCheck() { function testItReturnsErrorStateOnEmptyAPIResponseCodeDuringPremiumCheck() {
@ -314,4 +316,4 @@ class BridgeTest extends \MailPoetTest {
WPHelper::releaseAllFunctions(); WPHelper::releaseAllFunctions();
\ORM::raw_execute('TRUNCATE ' . Setting::$_table); \ORM::raw_execute('TRUNCATE ' . Setting::$_table);
} }
} }

View File

@ -48,6 +48,8 @@ jQuery('.toplevel_page_mailpoet-newsletters.menu-top-last')
var mailpoet_premium_version = <%= json_encode(mailpoet_premium_version()) %>; var mailpoet_premium_version = <%= json_encode(mailpoet_premium_version()) %>;
var mailpoet_analytics_enabled = <%= is_analytics_enabled() | json_encode %>; var mailpoet_analytics_enabled = <%= is_analytics_enabled() | json_encode %>;
var mailpoet_analytics_data = <%= json_encode(get_analytics_data()) %>; var mailpoet_analytics_data = <%= json_encode(get_analytics_data()) %>;
var mailpoet_analytics_public_id = <%= json_encode(get_analytics_public_id()) %>;
var mailpoet_analytics_new_public_id = <%= is_analytics_public_id_new() | json_encode %>;
// RFC 5322 standard; http://emailregex.com/ combined with https://google.github.io/closure-library/api/goog.format.EmailAddress.html#isValid // RFC 5322 standard; http://emailregex.com/ combined with https://google.github.io/closure-library/api/goog.format.EmailAddress.html#isValid
var mailpoet_email_regex = /(?=^[+a-zA-Z0-9_.!#$%&'*\/=?^`{|}~-]+@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,63}$)(?=^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,})))/; var mailpoet_email_regex = /(?=^[+a-zA-Z0-9_.!#$%&'*\/=?^`{|}~-]+@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,63}$)(?=^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,})))/;
</script> </script>