createAnalyticsMiddleware: Handle 'BATCH_ACTIONS'

This commit is contained in:
infinite-persistence 2021-10-22 09:50:11 +08:00
parent b7685a151d
commit 8c4224f1ce
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0

View file

@ -3,9 +3,25 @@ import analytics, { GA_DIMENSIONS } from 'analytics';
import * as ACTIONS from 'constants/action_types';
export function createAnalyticsMiddleware() {
return (/* { dispatch, getState } */) => (next: any) => (action: { type: string, data: any }) => {
switch (action.type) {
case ACTIONS.SUPPORT_TRANSACTION_COMPLETED:
return (/* { dispatch, getState } */) => (next: any) => (action: {
type: string,
data: any,
actions: Array<any>,
}) => {
if (action.type === 'BATCH_ACTIONS') {
action.actions.forEach((a) => handleAnalyticsForAction(a));
} else {
handleAnalyticsForAction(action);
}
return next(action);
};
}
function handleAnalyticsForAction(action: { type: string, data: any }) {
switch (action.type) {
case ACTIONS.SUPPORT_TRANSACTION_COMPLETED:
{
const { amount, type } = action.data;
analytics.reportEvent('spend_virtual_currency', {
// https://developers.google.com/analytics/devguides/collection/ga4/reference/events#spend_virtual_currency
@ -13,37 +29,39 @@ export function createAnalyticsMiddleware() {
virtual_currency_name: 'lbc',
item_name: type,
});
break;
}
break;
case ACTIONS.COMMENT_CREATE_COMPLETED:
analytics.reportEvent('comments', {
[GA_DIMENSIONS.TYPE]: 'create',
});
break;
case ACTIONS.COMMENT_CREATE_COMPLETED:
analytics.reportEvent('comments', {
[GA_DIMENSIONS.ACTION]: 'create',
});
break;
case ACTIONS.COMMENT_CREATE_FAILED:
analytics.reportEvent('comments', {
[GA_DIMENSIONS.TYPE]: 'create_fail',
});
break;
case ACTIONS.COMMENT_CREATE_FAILED:
analytics.reportEvent('comments', {
[GA_DIMENSIONS.ACTION]: 'create_fail',
});
break;
case ACTIONS.PUBLISH_SUCCESS:
case ACTIONS.PUBLISH_SUCCESS:
{
const { type } = action.data;
analytics.reportEvent('publish', {
[GA_DIMENSIONS.TYPE]: 'publish_success',
[GA_DIMENSIONS.ACTION]: 'publish_success',
[GA_DIMENSIONS.TYPE]: type,
});
break;
}
break;
case ACTIONS.PUBLISH_FAIL:
analytics.reportEvent('publish', {
[GA_DIMENSIONS.TYPE]: 'publish_fail',
});
break;
case ACTIONS.PUBLISH_FAIL:
analytics.reportEvent('publish', {
[GA_DIMENSIONS.ACTION]: 'publish_fail',
});
break;
default:
// Do nothing
break;
}
return next(action);
};
default:
// Do nothing
break;
}
}