Skip to content

Commit

Permalink
abstract analytics events
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes committed Oct 17, 2023
1 parent d234d9a commit 0826504
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 21 deletions.
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"vetur.experimental.templateInterpolationService": true,
"eslint.enable": true,
"eslint.format.enable": true,
"eslint.packageManager": "yarn",
"eslint.alwaysShowStatus": true,
"eslint.validate": ["javascript", "javascriptreact"],
"[javascript]": {
Expand Down
4 changes: 2 additions & 2 deletions src/components/DownloadButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { makeStyles } from '@material-ui/core/styles';
import GetAppIcon from '@material-ui/icons/GetApp';
import PropTypes from 'prop-types';
import React from 'react';
import firebase from '../config/firebase';
import analyticsEvent from '../shared/analyticsEvent';
import downloadPDF from '../shared/download-pdf';

const useStyles = makeStyles((theme) => ({
Expand All @@ -25,7 +25,7 @@ const DownloadButton = ({ item }) => {
onClick={() => {
downloadPDF(item);

firebase.analytics()?.logEvent('download_pdf', {
analyticsEvent('download_pdf', {
item_id: item.id,
});
}}
Expand Down
5 changes: 3 additions & 2 deletions src/components/SaveButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, { useEffect, useState } from 'react';
import { useAuthState } from 'react-firebase-hooks/auth';
import { connect } from 'react-redux';
import firebase from '../config/firebase';
import analyticsEvent from '../shared/analyticsEvent';
import { axiosDelete, axiosGet, axiosPut } from '../shared/axios';
import isBrowser from '../shared/browserCheck';
import { deleteBookmark, setBookmark } from '../store/actions';
Expand Down Expand Up @@ -55,7 +56,7 @@ const SaveButton = ({
setSaved(false);
deleteUserBookmark(resourceSlug);

firebase.analytics()?.logEvent('remove_bookmark', {
analyticsEvent('remove_bookmark', {
content_type: 'resource',
item_id: resourceSlug,
});
Expand All @@ -82,7 +83,7 @@ const SaveButton = ({
}).then(() => {
setUserBookmark(resourceSlug);

firebase.analytics()?.logEvent('add_bookmark', {
analyticsEvent('add_bookmark', {
content_type: 'resource',
item_id: resourceSlug,
});
Expand Down
4 changes: 2 additions & 2 deletions src/components/SearchModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { ArrowBack, Clear } from '@material-ui/icons';
import PropTypes from 'prop-types';
import React, { useState } from 'react';
import firebase from '../config/firebase';
import analyticsEvent from '../shared/analyticsEvent';
import { axiosGet } from '../shared/axios';
import useWindowDimensions from '../shared/dimensions';
import theme from '../styles/theme';
Expand All @@ -26,7 +26,7 @@ const Search = ({ shown, container, closeModal }) => {
const getSearchResults = async (param) => {
if (param.length >= 1) {
setSearchStatus('searching');
firebase.analytics()?.logEvent('search', { search_term: param });
analyticsEvent('search', { search_term: param });
const response = await axiosGet('resources', {
params: {
q: param,
Expand Down
5 changes: 3 additions & 2 deletions src/components/SignUpWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import React, { useEffect, useState } from 'react';
import { useAuthState } from 'react-firebase-hooks/auth';
import { connect } from 'react-redux';
import firebase, { uiConfig } from '../config/firebase';
import analyticsEvent from '../shared/analyticsEvent';
import { axiosGet, axiosPut } from '../shared/axios';
import isBrowser from '../shared/browserCheck';
import rollbar from '../shared/rollbar';
Expand Down Expand Up @@ -87,9 +88,9 @@ const SignUpWidget = ({

// Analytics
if (authResult.additionalUserInfo.isNewUser) {
firebase.analytics()?.logEvent('sign_up');
analyticsEvent('sign_up');
} else {
firebase.analytics()?.logEvent('login');
analyticsEvent('login');
}

const successResponse = async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Head from '../components/Head';
import Header from '../components/Header';
import Loading from '../components/Loading';
import firebase from '../config/firebase';
import analyticsEvent from '../shared/analyticsEvent';
import { axiosGet } from '../shared/axios';
import isBrowser from '../shared/browserCheck';
import useWindowDimensions from '../shared/dimensions';
Expand Down Expand Up @@ -123,7 +124,7 @@ function App({ Component, pageProps }) {
setIsLoading(true);
});
router.events.on('routeChangeComplete', () => {
firebase.analytics()?.logEvent('page_view');
analyticsEvent('page_view');
setIsLoading(false);
});
router.events.on('routeChangeError', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/resources/[resourceSlug].js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Head from '../../components/Head';
import Item from '../../components/Item';
import ResourceContents from '../../components/ResourceContents';
import firebase from '../../config/firebase';
import analyticsEvent from '../../shared/analyticsEvent';
import { axiosGet } from '../../shared/axios';
import isBrowser from '../../shared/browserCheck';

Expand Down Expand Up @@ -55,7 +56,7 @@ const ResourcePage = ({ propResource, propTheme, previewMode }) => {

useEffect(() => {
if (!previewMode && resource) {
firebase.analytics()?.logEvent('select_content', {
analyticsEvent('select_content', {
content_type: 'resource',
item_id: resource.slug,
});
Expand Down
21 changes: 12 additions & 9 deletions src/pages/resources/[resourceSlug]/items/[itemId].js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
Box,
Card,
CardActionArea,
CardContent, Grid, makeStyles,
Typography,
Box,
Card,
CardActionArea,
CardContent, Grid, makeStyles,
Typography,
} from '@material-ui/core';
import LinkUi from '@material-ui/core/Link';
import { ArrowBack, ArrowForward } from '@material-ui/icons';
Expand All @@ -15,6 +15,7 @@ import { useAuthState } from 'react-firebase-hooks/auth';
import Head from '../../../../components/Head';
import Item from '../../../../components/Item';
import firebase from '../../../../config/firebase';
import analyticsEvent from '../../../../shared/analyticsEvent';
import { axiosGet } from '../../../../shared/axios';
import isBrowser from '../../../../shared/browserCheck';

Expand Down Expand Up @@ -84,10 +85,12 @@ const ItemPage = ({ propResource, previewMode }) => {

useEffect(() => {
if (!previewMode && item) {
firebase.analytics()?.logEvent('select_content', {
content_type: 'resource_content_item',
item_id: `${resource.slug}/${itemId}`,
});
analyticsEvent(
'select_content', {
content_type: 'resource_content_item',
item_id: `${resource.slug}/${itemId}`,
},
);
}
}, [item, itemId, resourceSlug]);

Expand Down
3 changes: 2 additions & 1 deletion src/pages/themes/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import ResourceCard from '../../components/ResourceCard';
import SearchModal from '../../components/SearchModal';
import SignUpPrompt from '../../components/SignUpPrompt';
import firebase from '../../config/firebase';
import analyticsEvent from '../../shared/analyticsEvent';
import { axiosGet } from '../../shared/axios';
import isBrowser from '../../shared/browserCheck';
import richTextHelper from '../../shared/rich-text';
Expand Down Expand Up @@ -112,7 +113,7 @@ const ThemePage = ({

useEffect(() => {
if (!previewMode && theme) {
firebase.analytics()?.logEvent('select_content', {
analyticsEvent('select_content', {
content_type: 'theme',
item_id: theme.slug,
});
Expand Down
7 changes: 7 additions & 0 deletions src/shared/analyticsEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import firebase from '../config/firebase';

export default function analyticsEvent(event, params) {
if (process.env.NEXT_PUBLIC_ENV === 'production') {
firebase.analytics().logEvent(event, params);
}
}

0 comments on commit 0826504

Please sign in to comment.