Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Pass events to ga
Browse files Browse the repository at this point in the history
  • Loading branch information
osahyoun committed Jul 30, 2018
1 parent 778c052 commit 57a0d68
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/javascript/member-facing/backbone/action_form.js
Expand Up @@ -5,6 +5,7 @@ import React from 'react';
import { render } from 'react-dom';
import _ from 'lodash';
import Backbone from 'backbone';
import ee from '../../shared/pub_sub';
import ErrorDisplay from '../../shared/show_errors';
import MobileCheck from './mobile_check';
import GlobalEvents from '../../shared/global_events';
Expand Down Expand Up @@ -324,6 +325,7 @@ const ActionForm = Backbone.View.extend({
handleSuccess(e, data) {
// FIXME: we should return consistently from the backend
// FIXME: we should not rely on mutating function arguments of unkown type
ee.emit('action:submitted_success');
if (typeof data === 'object') data.petitionForm = this.formValues();
Backbone.trigger('form:submitted', e, data);
},
Expand Down
1 change: 1 addition & 0 deletions app/javascript/util/event_tracking.js
Expand Up @@ -10,6 +10,7 @@ import { logEvent } from './log_event';
'member:reset',
'petition:submitted',
'fundraiser:transaction_submitted',
'action:submitted_success',
].forEach(eventName => {
const callback = (e, ...rest) => logEvent(eventName, ...rest);
ee.on(eventName, callback);
Expand Down
38 changes: 38 additions & 0 deletions app/javascript/util/log_event.js
Expand Up @@ -13,4 +13,42 @@ export const logEvent = (eventName: string, payload: any) => {
};

if (window.TRACK_USER_ACTIONS) window.mixpanel.track(eventName, opts);

if (window.ga) logToGa(eventName, payload);
};

const getEventData = (eventName: string, data: any) => {
switch (eventName) {
case 'action:submitted_success':
return ['action', 'submitted_success'];
case '@@chmp:consent:change_country':
return ['gdpr', 'change_country', data.countryCode];
case '@@chmp:consent:change_consent':
return ['gdpr', 'change_consent', data.consented ? 'true' : 'false'];
case 'change_amount':
return ['fundraising', 'change_amount', null, parseFloat(data.payload)];
case 'set_store_in_vault':
return [
'fundraising',
'set_store_in_valut',
data.payload ? 'true' : 'false',
];
case 'set_recurring':
return ['fundraising', 'set_recurring', data.payload ? 'true' : 'false'];
case 'fundraiser:transaction_submitted':
return [
'fundraising',
'transaction_submitted',
null,
parseFloat(data.amount),
];
default:
return false;
}
};

const logToGa = (eventName: string, data: any) => {
const eventData = getEventData(eventName, data);

window.ga('send', 'event', ...eventData);
};

0 comments on commit 57a0d68

Please sign in to comment.