Notes on Offline Events
import { GoogleAnalytics } from 'redux-beacon/targets/google-analytics';
import { TRACKING_ID } from './constants';
import axios from 'axios';
const higherOrderTarget = (events) => {
// filter out any events that were saved offline
const currentEvents = events.filter(event => {
if (event.timeSaved === undefined) {
return true; // keep these events
}
// calculate the time since the event was saved
const timeSinceEventSaved = Date.now() - event.timeSaved;
// post the offline events to google analytics
ga(function(tracker) {
const url = 'https://www.google-analytics.com/collect';
const params = [
'v=1',
`tid=${TRACKING_ID}`,
`cid=${tracker.get('clientId')}`,
`t=${event.hitType}`,
`dp=${event.page}`,
`qt=${timeSinceEventSaved}`,
].join('&');
axios.post(`${url}?${params}`);
});
// push any events that were not saved offline directly to the target
GoogleAnalytics(currentEvents);
};Last updated