mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
Refactor to use object method of sending google data
This commit is contained in:
@@ -59,14 +59,7 @@ describe('analytics', function() {
|
|||||||
|
|
||||||
describe('track', function() {
|
describe('track', function() {
|
||||||
|
|
||||||
var event_type = 'Cron';
|
var analyticsData, event_type;
|
||||||
var analyticsData = {
|
|
||||||
category: 'behavior',
|
|
||||||
uuid: 'unique-user-id',
|
|
||||||
resting: true,
|
|
||||||
cronCount: 5
|
|
||||||
}
|
|
||||||
|
|
||||||
var analytics = rewire('../../website/src/analytics');
|
var analytics = rewire('../../website/src/analytics');
|
||||||
var initializedAnalytics;
|
var initializedAnalytics;
|
||||||
|
|
||||||
@@ -75,6 +68,14 @@ describe('analytics', function() {
|
|||||||
initializedAnalytics = analytics({amplitudeToken: 'token'});
|
initializedAnalytics = analytics({amplitudeToken: 'token'});
|
||||||
analytics.__set__('amplitude.track', amplitudeTrack);
|
analytics.__set__('amplitude.track', amplitudeTrack);
|
||||||
analytics.__set__('ga.event', googleEvent);
|
analytics.__set__('ga.event', googleEvent);
|
||||||
|
|
||||||
|
event_type = 'Cron';
|
||||||
|
analyticsData = {
|
||||||
|
category: 'behavior',
|
||||||
|
uuid: 'unique-user-id',
|
||||||
|
resting: true,
|
||||||
|
cronCount: 5
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
context('Amplitude', function() {
|
context('Amplitude', function() {
|
||||||
@@ -103,10 +104,9 @@ describe('analytics', function() {
|
|||||||
purchased: { plan: { planId: 'foo-plan' } }
|
purchased: { plan: { planId: 'foo-plan' } }
|
||||||
};
|
};
|
||||||
|
|
||||||
var analyticsDataWithUser = _.cloneDeep(analyticsData);
|
analyticsData.user = user;
|
||||||
analyticsDataWithUser.user = user;
|
|
||||||
|
|
||||||
initializedAnalytics.track(event_type, analyticsDataWithUser);
|
initializedAnalytics.track(event_type, analyticsData);
|
||||||
|
|
||||||
expect(amplitudeTrack).to.be.calledOnce;
|
expect(amplitudeTrack).to.be.calledOnce;
|
||||||
expect(amplitudeTrack).to.be.calledWith({
|
expect(amplitudeTrack).to.be.calledWith({
|
||||||
@@ -137,88 +137,45 @@ describe('analytics', function() {
|
|||||||
initializedAnalytics.track(event_type, analyticsData);
|
initializedAnalytics.track(event_type, analyticsData);
|
||||||
|
|
||||||
expect(googleEvent).to.be.calledOnce;
|
expect(googleEvent).to.be.calledOnce;
|
||||||
expect(googleEvent).to.be.calledWith(
|
expect(googleEvent).to.be.calledWith({
|
||||||
'behavior',
|
ec: 'behavior',
|
||||||
'Cron',
|
ea: 'Cron'
|
||||||
'Label Not Specified'
|
});
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('if goldCost property is provided, use as label', function() {
|
it('if itemKey property is provided, use as label', function() {
|
||||||
var data = _.cloneDeep(analyticsData);
|
analyticsData.itemKey = 'some item';
|
||||||
data.goldCost = 4;
|
|
||||||
|
|
||||||
initializedAnalytics.track(event_type, data);
|
initializedAnalytics.track(event_type, analyticsData);
|
||||||
|
|
||||||
expect(googleEvent).to.be.calledOnce;
|
expect(googleEvent).to.be.calledOnce;
|
||||||
expect(googleEvent).to.be.calledWith(
|
expect(googleEvent).to.be.calledWith({
|
||||||
'behavior',
|
ec: 'behavior',
|
||||||
'Cron',
|
ea: 'Cron',
|
||||||
4
|
el: 'some item'
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('if gemCost property is provided, use as label (overrides goldCost)', function() {
|
|
||||||
var data = _.cloneDeep(analyticsData);
|
|
||||||
data.goldCost = 10;
|
|
||||||
data.itemKey = 50;
|
|
||||||
|
|
||||||
initializedAnalytics.track(event_type, data);
|
|
||||||
|
|
||||||
expect(googleEvent).to.be.calledOnce;
|
|
||||||
expect(googleEvent).to.be.calledWith(
|
|
||||||
'behavior',
|
|
||||||
'Cron',
|
|
||||||
50
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('if itemKey property is provided, use as label (overrides gem/goldCost)', function() {
|
|
||||||
var data = _.cloneDeep(analyticsData);
|
|
||||||
data.goldCost = 5;
|
|
||||||
data.gemCost = 50;
|
|
||||||
data.itemKey = 'some item';
|
|
||||||
|
|
||||||
initializedAnalytics.track(event_type, data);
|
|
||||||
|
|
||||||
expect(googleEvent).to.be.calledOnce;
|
|
||||||
expect(googleEvent).to.be.calledWith(
|
|
||||||
'behavior',
|
|
||||||
'Cron',
|
|
||||||
'some item'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('if gaLabel property is provided, use as label (overrides itemKey)', function() {
|
it('if gaLabel property is provided, use as label (overrides itemKey)', function() {
|
||||||
var data = _.cloneDeep(analyticsData);
|
analyticsData.value = 'some value';
|
||||||
data.value = 'some value';
|
analyticsData.itemKey = 'some item';
|
||||||
data.itemKey = 'some item';
|
analyticsData.gaLabel = 'some label';
|
||||||
data.gaLabel = 'some label';
|
|
||||||
|
|
||||||
initializedAnalytics.track(event_type, data);
|
initializedAnalytics.track(event_type, analyticsData);
|
||||||
|
|
||||||
expect(googleEvent).to.be.calledOnce;
|
expect(googleEvent).to.be.calledOnce;
|
||||||
expect(googleEvent).to.be.calledWith(
|
expect(googleEvent).to.be.calledWith({
|
||||||
'behavior',
|
ec: 'behavior',
|
||||||
'Cron',
|
ea: 'Cron',
|
||||||
'some label'
|
el: 'some label'
|
||||||
);
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('trackPurchase', function() {
|
describe('trackPurchase', function() {
|
||||||
|
|
||||||
var purchaseData = {
|
var purchaseData;
|
||||||
uuid: 'user-id',
|
|
||||||
sku: 'paypal-checkout',
|
|
||||||
paymentMethod: 'PayPal',
|
|
||||||
itemPurchased: 'Gems',
|
|
||||||
purchaseValue: 8,
|
|
||||||
purchaseType: 'checkout',
|
|
||||||
gift: false,
|
|
||||||
quantity: 1
|
|
||||||
}
|
|
||||||
|
|
||||||
var analytics = rewire('../../website/src/analytics');
|
var analytics = rewire('../../website/src/analytics');
|
||||||
var initializedAnalytics;
|
var initializedAnalytics;
|
||||||
@@ -229,13 +186,24 @@ describe('analytics', function() {
|
|||||||
analytics.__set__('amplitude.track', amplitudeTrack);
|
analytics.__set__('amplitude.track', amplitudeTrack);
|
||||||
analytics.__set__('ga.event', googleEvent);
|
analytics.__set__('ga.event', googleEvent);
|
||||||
analytics.__set__('ga.transaction', googleTransaction);
|
analytics.__set__('ga.transaction', googleTransaction);
|
||||||
|
|
||||||
|
purchaseData = {
|
||||||
|
uuid: 'user-id',
|
||||||
|
sku: 'paypal-checkout',
|
||||||
|
paymentMethod: 'PayPal',
|
||||||
|
itemPurchased: 'Gems',
|
||||||
|
purchaseValue: 8,
|
||||||
|
purchaseType: 'checkout',
|
||||||
|
gift: false,
|
||||||
|
quantity: 1
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
context('Amplitude', function() {
|
context('Amplitude', function() {
|
||||||
|
|
||||||
it('calls amplitude.track', function() {
|
it('calls amplitude.track', function() {
|
||||||
var data = _.cloneDeep(purchaseData);
|
initializedAnalytics.trackPurchase(purchaseData);
|
||||||
initializedAnalytics.trackPurchase(data);
|
|
||||||
|
|
||||||
expect(amplitudeTrack).to.be.calledOnce;
|
expect(amplitudeTrack).to.be.calledOnce;
|
||||||
expect(amplitudeTrack).to.be.calledWith({
|
expect(amplitudeTrack).to.be.calledWith({
|
||||||
@@ -258,21 +226,19 @@ describe('analytics', function() {
|
|||||||
context('Google Analytics', function() {
|
context('Google Analytics', function() {
|
||||||
|
|
||||||
it('calls ga.event', function() {
|
it('calls ga.event', function() {
|
||||||
var data = _.cloneDeep(purchaseData);
|
initializedAnalytics.trackPurchase(purchaseData);
|
||||||
initializedAnalytics.trackPurchase(data);
|
|
||||||
|
|
||||||
expect(googleEvent).to.be.calledOnce;
|
expect(googleEvent).to.be.calledOnce;
|
||||||
expect(googleEvent).to.be.calledWith(
|
expect(googleEvent).to.be.calledWith({
|
||||||
'commerce',
|
ec: 'commerce',
|
||||||
'checkout',
|
ea: 'checkout',
|
||||||
'PayPal',
|
el: 'PayPal',
|
||||||
8
|
ev: 8
|
||||||
);
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('calls ga.transaction', function() {
|
it('calls ga.transaction', function() {
|
||||||
var data = _.cloneDeep(purchaseData);
|
initializedAnalytics.trackPurchase(purchaseData);
|
||||||
initializedAnalytics.trackPurchase(data);
|
|
||||||
|
|
||||||
expect(googleTransaction).to.be.calledOnce;
|
expect(googleTransaction).to.be.calledOnce;
|
||||||
expect(googleTransaction).to.be.calledWith(
|
expect(googleTransaction).to.be.calledWith(
|
||||||
@@ -291,9 +257,8 @@ describe('analytics', function() {
|
|||||||
|
|
||||||
it('appends gift to variation of ga.transaction.item if gift is true', function() {
|
it('appends gift to variation of ga.transaction.item if gift is true', function() {
|
||||||
|
|
||||||
var data = _.cloneDeep(purchaseData);
|
purchaseData.gift = true;
|
||||||
data.gift = true;
|
initializedAnalytics.trackPurchase(purchaseData);
|
||||||
initializedAnalytics.trackPurchase(data);
|
|
||||||
|
|
||||||
expect(googleItem).to.be.calledOnce;
|
expect(googleItem).to.be.calledOnce;
|
||||||
expect(googleItem).to.be.calledWith(
|
expect(googleItem).to.be.calledWith(
|
||||||
|
|||||||
@@ -31,15 +31,20 @@ function _sendDataToAmplitude(eventType, data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function _sendDataToGoogle(eventType, data) {
|
function _sendDataToGoogle(eventType, data) {
|
||||||
var category = data.category;
|
var eventData = {
|
||||||
var label = _generateLabelForGoogleAnalytics(data);
|
ec: data.category,
|
||||||
|
ea: eventType
|
||||||
|
}
|
||||||
|
|
||||||
ga.event(category, eventType, label).send();
|
var label = _generateLabelForGoogleAnalytics(data);
|
||||||
|
if(label) { eventData.el = label; }
|
||||||
|
|
||||||
|
ga.event(eventData).send();
|
||||||
}
|
}
|
||||||
|
|
||||||
function _generateLabelForGoogleAnalytics(data) {
|
function _generateLabelForGoogleAnalytics(data) {
|
||||||
var label = 'Label Not Specified';
|
var label;
|
||||||
var POSSIBLE_LABELS = ['gaLabel', 'itemKey', 'gemCost', 'goldCost'];
|
var POSSIBLE_LABELS = ['gaLabel', 'itemKey'];
|
||||||
|
|
||||||
_(POSSIBLE_LABELS).each(function(key) {
|
_(POSSIBLE_LABELS).each(function(key) {
|
||||||
if(data[key]) {
|
if(data[key]) {
|
||||||
@@ -114,8 +119,14 @@ function _sendPurchaseDataToGoogle(data) {
|
|||||||
var variation = type;
|
var variation = type;
|
||||||
if(data.gift) variation += ' - Gift';
|
if(data.gift) variation += ' - Gift';
|
||||||
|
|
||||||
ga.event('commerce', type, label, price)
|
var eventData = {
|
||||||
.send();
|
ec: 'commerce',
|
||||||
|
ea: type,
|
||||||
|
el: label,
|
||||||
|
ev: price
|
||||||
|
};
|
||||||
|
|
||||||
|
ga.event(eventData).send();
|
||||||
|
|
||||||
ga.transaction(data.uuid, price)
|
ga.transaction(data.uuid, price)
|
||||||
.item(price, qty, sku, itemKey, variation)
|
.item(price, qty, sku, itemKey, variation)
|
||||||
|
|||||||
Reference in New Issue
Block a user