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 }) => {
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,29 +29,34 @@ export function createAnalyticsMiddleware() {
virtual_currency_name: 'lbc',
item_name: type,
});
}
break;
case ACTIONS.COMMENT_CREATE_COMPLETED:
analytics.reportEvent('comments', {
[GA_DIMENSIONS.TYPE]: 'create',
[GA_DIMENSIONS.ACTION]: 'create',
});
break;
case ACTIONS.COMMENT_CREATE_FAILED:
analytics.reportEvent('comments', {
[GA_DIMENSIONS.TYPE]: 'create_fail',
[GA_DIMENSIONS.ACTION]: 'create_fail',
});
break;
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;
case ACTIONS.PUBLISH_FAIL:
analytics.reportEvent('publish', {
[GA_DIMENSIONS.TYPE]: 'publish_fail',
[GA_DIMENSIONS.ACTION]: 'publish_fail',
});
break;
@ -43,7 +64,4 @@ export function createAnalyticsMiddleware() {
// Do nothing
break;
}
return next(action);
};
}