Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plausible events for favorite features #2579

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useFavoriteFeaturesApi } from 'hooks/api/actions/useFavoriteFeaturesApi
import { FavoriteIconCell } from './FavoriteIconCell/FavoriteIconCell';
import { FavoriteIconHeader } from './FavoriteIconHeader/FavoriteIconHeader';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import { usePlausibleTracker } from '../../../hooks/usePlausibleTracker';

export const featuresPlaceholder: FeatureSchema[] = Array(15).fill({
name: 'Name of the feature',
Expand Down Expand Up @@ -172,7 +173,7 @@ export const FeatureToggleListTable: VFC = () => {
accessor: 'description',
},
],
[isFavoritesPinned]
[isFavoritesPinned, uiConfig?.flags?.favorites]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed a small bug, that favorites were not updating, due to missing dependency.

);

const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import useToast from 'hooks/useToast';
import { formatUnknownError } from 'utils/formatUnknownError';
import { useFeatures } from 'hooks/api/getters/useFeatures/useFeatures';
import useAPI from '../useApi/useApi';
import { usePlausibleTracker } from '../../../usePlausibleTracker';

export const useFavoriteFeaturesApi = () => {
const { makeRequest, createRequest, errors, loading } = useAPI({
propagateErrors: true,
});
const { setToastData, setToastApiError } = useToast();
const { refetchFeatures } = useFeatures();
const { trackEvent } = usePlausibleTracker();

const favorite = useCallback(
async (projectId: string, featureName: string) => {
Expand All @@ -27,6 +29,11 @@ export const useFavoriteFeaturesApi = () => {
title: 'Toggle added to favorites',
type: 'success',
});
trackEvent('favorite', {
props: {
eventType: `feature favorited`,
},
});
refetchFeatures();
} catch (error) {
setToastApiError(formatUnknownError(error));
Expand All @@ -51,6 +58,11 @@ export const useFavoriteFeaturesApi = () => {
title: 'Toggle removed from favorites',
type: 'success',
});
trackEvent('favorite', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we have this event in the catch block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is in try block. Hmm?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry weird github formatting

props: {
eventType: `feature unfavorited`,
},
});
refetchFeatures();
} catch (error) {
setToastApiError(formatUnknownError(error));
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/hooks/usePinnedFavorites.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMemo, useState } from 'react';
import { sortTypes } from 'utils/sortTypes';
import type { Row, SortByFn } from 'react-table';
import { usePlausibleTracker } from './usePlausibleTracker';

type WithFavorite = {
favorite: boolean;
Expand Down Expand Up @@ -33,8 +34,14 @@ export const sortTypesWithFavorites: Record<
*/
export const usePinnedFavorites = (initialState = false) => {
const [isFavoritesPinned, setIsFavoritesPinned] = useState(initialState);
const { trackEvent } = usePlausibleTracker();

const onChangeIsFavoritePinned = () => {
trackEvent('favorite', {
props: {
eventType: `features ${isFavoritesPinned ? 'un' : ''}pinned `,
},
});
setIsFavoritesPinned(!isFavoritesPinned);
};

Expand Down
6 changes: 5 additions & 1 deletion frontend/src/hooks/usePlausibleTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { EventOptions, PlausibleOptions } from 'plausible-tracker';
* @see https://plausible.io/docs/custom-event-goals#2-create-a-custom-event-goal-in-your-plausible-analytics-account
* @example `'download | 'invite' | 'signup'`
**/
type CustomEvents = 'invite' | 'upgrade_plan_clicked' | 'change_request';
type CustomEvents =
| 'invite'
| 'upgrade_plan_clicked'
| 'change_request'
| 'favorite';

export const usePlausibleTracker = () => {
const plausible = useContext(PlausibleContext);
Expand Down