Skip to content

Commit 72fff17

Browse files
committed
feat: support analytics from Q&A
closes #51
1 parent 0227d9d commit 72fff17

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

plugins/qeta/src/components/AskForm/AskForm.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { configApiRef, useApi } from '@backstage/core-plugin-api';
1+
import { configApiRef, useAnalytics, useApi } from '@backstage/core-plugin-api';
22
import { Button, TextField } from '@material-ui/core';
33
import { Alert, Autocomplete } from '@material-ui/lab';
44
import React, { useEffect, useMemo } from 'react';
@@ -79,6 +79,7 @@ export const AskForm = (props: {
7979
const { id, entity, onPost } = props;
8080
const base_path = useBasePath();
8181
const navigate = useNavigate();
82+
const analytics = useAnalytics();
8283
const [entityRef, setEntityRef] = React.useState(entity);
8384
const [values, setValues] = React.useState(getDefaultValues());
8485
const [error, setError] = React.useState(false);
@@ -122,6 +123,7 @@ export const AskForm = (props: {
122123
return;
123124
}
124125
reset();
126+
analytics.captureEvent('edit', 'question');
125127
if (onPost) {
126128
onPost(q);
127129
} else {
@@ -138,6 +140,7 @@ export const AskForm = (props: {
138140
setError(true);
139141
return;
140142
}
143+
analytics.captureEvent('post', 'question');
141144
reset();
142145
navigate(`${base_path}/qeta/questions/${q.id}`);
143146
})

plugins/qeta/src/components/CommentSection/CommentSection.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Box, Button, Grid, TextField } from '@material-ui/core';
33
import { Link } from '@backstage/core-components';
44
import { AnswerResponse, qetaApiRef, QuestionResponse } from '../../api';
55
import { Controller, useForm } from 'react-hook-form';
6-
import { useApi } from '@backstage/core-plugin-api';
6+
import { useAnalytics, useApi } from '@backstage/core-plugin-api';
77
import { CommentList } from './CommentList';
88

99
export const CommentSection = (props: {
@@ -16,6 +16,7 @@ export const CommentSection = (props: {
1616
answer?: AnswerResponse;
1717
}) => {
1818
const { answer, question, onCommentPost, onCommentDelete } = props;
19+
const analytics = useAnalytics();
1920
const qetaApi = useApi(qetaApiRef);
2021
const [formVisible, setFormVisible] = useState(false);
2122
const {
@@ -29,6 +30,7 @@ export const CommentSection = (props: {
2930
if (answer) {
3031
qetaApi.commentAnswer(question.id, answer.id, data.content).then(a => {
3132
setFormVisible(false);
33+
analytics.captureEvent('comment', 'answer');
3234
reset();
3335
onCommentPost(question, a);
3436
});
@@ -37,6 +39,7 @@ export const CommentSection = (props: {
3739

3840
qetaApi.commentQuestion(question.id, data.content).then(q => {
3941
setFormVisible(false);
42+
analytics.captureEvent('comment', 'question');
4043
reset();
4144
onCommentPost(q);
4245
});

plugins/qeta/src/components/QuestionPage/AnswerForm.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { WarningPanel } from '@backstage/core-components';
2-
import { Typography, Button } from '@material-ui/core';
2+
import { Button, Typography } from '@material-ui/core';
33
import React, { useEffect } from 'react';
4-
import { useApi } from '@backstage/core-plugin-api';
4+
import { useAnalytics, useApi } from '@backstage/core-plugin-api';
55
import {
66
AnswerRequest,
77
AnswerResponse,
@@ -25,6 +25,7 @@ export const AnswerForm = (props: {
2525
}) => {
2626
const { question, onPost, id } = props;
2727
const [values, setValues] = React.useState(getDefaultValues(question.id));
28+
const analytics = useAnalytics();
2829
const [error, setError] = React.useState(false);
2930
const qetaApi = useApi(qetaApiRef);
3031
const styles = useStyles();
@@ -48,6 +49,7 @@ export const AnswerForm = (props: {
4849
setError(true);
4950
return;
5051
}
52+
analytics.captureEvent('edit', 'answer');
5153
reset();
5254
onPost(a);
5355
})
@@ -62,6 +64,7 @@ export const AnswerForm = (props: {
6264
setError(true);
6365
return;
6466
}
67+
analytics.captureEvent('post', 'answer');
6568
reset();
6669
onPost(a);
6770
})

plugins/qeta/src/components/QuestionPage/VoteButtons.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import ArrowDownward from '@material-ui/icons/ArrowDownward';
1010
import ArrowUpward from '@material-ui/icons/ArrowUpward';
1111
import Check from '@material-ui/icons/Check';
1212
import React from 'react';
13-
import { useApi } from '@backstage/core-plugin-api';
13+
import { useAnalytics, useApi } from '@backstage/core-plugin-api';
1414

1515
export const VoteButtons = (props: {
1616
entity: QuestionResponse | AnswerResponse;
1717
question?: QuestionResponse;
1818
}) => {
1919
const [ownVote, setOwnVote] = React.useState(props.entity.ownVote ?? 0);
20+
const analytics = useAnalytics();
2021
const isCorrectAnswer =
2122
'questionId' in props.entity ? props.entity.correct : false;
2223
const [correct, setCorrect] = React.useState(isCorrectAnswer);
@@ -32,11 +33,13 @@ export const VoteButtons = (props: {
3233
if (isQuestion) {
3334
qetaApi.voteQuestionUp(entity.id).then(response => {
3435
setOwnVote(1);
36+
analytics.captureEvent('vote', 'question', { value: 1 });
3537
setEntity(response);
3638
});
3739
} else if ('questionId' in entity) {
3840
qetaApi.voteAnswerUp(entity.questionId, entity.id).then(response => {
3941
setOwnVote(1);
42+
analytics.captureEvent('vote', 'answer', { value: 1 });
4043
setEntity(response);
4144
});
4245
}
@@ -46,11 +49,13 @@ export const VoteButtons = (props: {
4649
if (isQuestion) {
4750
qetaApi.voteQuestionDown(entity.id).then(response => {
4851
setOwnVote(-1);
52+
analytics.captureEvent('vote', 'question', { value: -1 });
4953
setEntity(response);
5054
});
5155
} else if ('questionId' in entity) {
5256
qetaApi.voteAnswerDown(entity.questionId, entity.id).then(response => {
5357
setOwnVote(-1);
58+
analytics.captureEvent('vote', 'answer', { value: -1 });
5459
setEntity(response);
5560
});
5661
}

0 commit comments

Comments
 (0)