Skip to content

Commit

Permalink
Added react hook for App Insights (#1574)
Browse files Browse the repository at this point in the history
* added config for disabling cfg

* revert comment

* created hook to use app insights, removed code duplication

* removed comment

* added cfg config

* added comment
  • Loading branch information
babakamyljanovssw authored Oct 16, 2024
1 parent bd206cf commit 23bb717
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 233 deletions.
16 changes: 5 additions & 11 deletions gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import wrapPageElementWithTransition from './src/helpers/wrapPageElement.js';
import { ApplicationInsights } from '@microsoft/applicationinsights-web';

require('gatsby-remark-vscode/styles.css');

const appInsights = new ApplicationInsights({
config: {
connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING,
},
});
appInsights.loadAppInsights();
appInsights.addTelemetryInitializer((item) => {
item.tags['ai.cloud.role'] = 'SSW.Rules-StaticClientPage';
});
appInsights.trackPageView(); // Manually call trackPageView to establish the current user/session/pageview
export const onRouteUpdate = () => {
if (window.appInsights && window.appInsights.trackPage) {
window.appInsights.trackPage();
}
};

// Page Transitions
export const wrapPageElement = wrapPageElementWithTransition;
17 changes: 4 additions & 13 deletions src/components/acknowledgements/acknowledgements.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
import React from 'react';
import PropTypes from 'prop-types';
import PlaceHolderImage from '../../images/ssw-employee-profile-placeholder-sketch.jpg';
import { ApplicationInsights } from '@microsoft/applicationinsights-web';

const appInsights = new ApplicationInsights({
config: {
connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING,
},
});
appInsights.loadAppInsights();
import useAppInsights from '../../hooks/useAppInsights';

const Acknowledgements = ({ authors, location }) => {
const { trackTrace } = useAppInsights();

function ProfileBadge(props) {
const author = props.author;
return (
Expand All @@ -29,12 +24,8 @@ const Acknowledgements = ({ authors, location }) => {

function ProfileImg({ author }) {
const { noimage, img, title, url } = author;

if (!title) {
appInsights.trackTrace({
message: `Profile title is missing at ${location.href}`,
severityLevel: 2,
});
trackTrace(`Profile title is missing at ${location.href}`, 2);
}

const getImgSource = () => {
Expand Down
25 changes: 6 additions & 19 deletions src/components/bookmark/bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ import {
} from '../../services/apiService';
import { useAuthService } from '../../services/authService';
import PropTypes from 'prop-types';
import { ApplicationInsights } from '@microsoft/applicationinsights-web';

const appInsights = new ApplicationInsights({
config: {
connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING,
},
});
import useAppInsights from '../../hooks/useAppInsights';

const Bookmark = (props) => {
const { ruleId } = props;
Expand All @@ -25,17 +19,16 @@ const Bookmark = (props) => {
const { isAuthenticated, user, loginWithRedirect } = useAuth0();
const { fetchIdToken } = useAuthService();

const { trackException } = useAppInsights();

useEffect(() => {
if (isAuthenticated) {
GetBookmarksForUser(user.sub, ruleId)
.then((success) => {
setBookmarked(success.bookmarkStatus);
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
});
}
}, [user, change]);
Expand All @@ -51,20 +44,14 @@ const Bookmark = (props) => {
setChange(change + 1);
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
})
: RemoveBookmark({ ruleGuid: ruleId, UserId: user.sub }, jwt)
.then(() => {
setChange(change + 1);
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
});
} else {
if (window.confirm('Sign in to bookmark this rule')) {
Expand Down
19 changes: 5 additions & 14 deletions src/components/comments-not-connected/comments-not-connected.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import { useAuthService } from '../../services/authService';
import DisqusIcon from '-!svg-react-loader!../../images/disqusIcon.svg';

import { useAuth0 } from '@auth0/auth0-react';
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
const appInsights = new ApplicationInsights({
config: {
connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING,
},
});
import useAppInsights from '../../hooks/useAppInsights';

const CommentsNotConnected = ({ userCommentsConnected, setState, state }) => {
const [disqusUsername, setDisqusUsername] = useState();
Expand All @@ -24,6 +19,8 @@ const CommentsNotConnected = ({ userCommentsConnected, setState, state }) => {
const { user } = useAuth0();
const { fetchIdToken } = useAuthService();

const { trackException } = useAppInsights();

function connectAccounts() {
if (disqusUsername) {
GetDisqusUser(disqusUsername)
Expand All @@ -48,17 +45,11 @@ const CommentsNotConnected = ({ userCommentsConnected, setState, state }) => {
}
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
});
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
});
}
}
Expand Down
16 changes: 4 additions & 12 deletions src/components/header/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@ import {
faPlusCircle,
faQuestionCircle,
} from '@fortawesome/free-solid-svg-icons';
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
import { MegaMenuLayout } from 'ssw.megamenu';
import { graphql, useStaticQuery, navigate } from 'gatsby';
import classNames from 'classnames';

const appInsights = new ApplicationInsights({
config: {
connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING,
},
});

appInsights.loadAppInsights();
import useAppInsights from '../../hooks/useAppInsights';

// Example of a component-specific page transition
const AnimatedContainer = posed.div({
Expand Down Expand Up @@ -109,6 +101,8 @@ const Header = ({ displayActions }) => {
};

const ActionButtons = () => {
const { trackEvent } = useAppInsights();

return (
<div className="action-btn-container max-sm:order-2 flex justify-items-end align-middle">
<Tooltip text="Try out RulesGPT" showDelay={3000} hideDelay={18000}>
Expand All @@ -118,9 +112,7 @@ const ActionButtons = () => {
href="https://rulesgpt.ssw.com.au"
className="action-btn-link-underlined"
onClick={() => {
appInsights.trackEvent({
name: 'RulesGPTButtonPressed',
});
trackEvent('RulesGPTButtonPressed');
}}
>
<GPTIcon className="group group-hover:[&>circle]:fill-ssw-red" />
Expand Down
45 changes: 9 additions & 36 deletions src/components/profile-content/profile-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import MD from 'gatsby-custom-md';
import GreyBox from '../greybox/greybox';
import { useAuth0 } from '@auth0/auth0-react';
import { Filter } from '../profile-filter-menu/profile-filter-menu';
import { ApplicationInsights } from '@microsoft/applicationinsights-web';

import CommentsNotConnected from '../comments-not-connected/comments-not-connected';
import DisableDisqusPrivacy from '../disable-disqus-privacy/disable-disqus-privacy';
Expand All @@ -33,12 +32,7 @@ import {
faFileLines,
faBook,
} from '@fortawesome/free-solid-svg-icons';

const appInsights = new ApplicationInsights({
config: {
connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING,
},
});
import useAppInsights from '../../hooks/useAppInsights';

const ProfileContent = (props) => {
const [bookmarkedRules, setBookmarkedRules] = useState([]);
Expand All @@ -53,7 +47,7 @@ const ProfileContent = (props) => {
const [viewStyle, setViewStyle] = useState('titleOnly');
const { user, isAuthenticated } = useAuth0();
const { fetchIdToken } = useAuthService();

const { trackException } = useAppInsights();
const handleOptionChange = (e) => {
setViewStyle(e.target.value);
};
Expand All @@ -71,20 +65,14 @@ const ProfileContent = (props) => {
props.setState(props.state + 1);
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
})
: RemoveReaction({ ruleGuid: ruleGuid, UserId: user.sub }, jwt)
.then(() => {
setChange(change + 1);
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
});
}
}
Expand All @@ -109,10 +97,7 @@ const ProfileContent = (props) => {
props.setBookmarkedRulesCount(bookmarkedRulesSpread.length);
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
});
}

Expand Down Expand Up @@ -144,10 +129,7 @@ const ProfileContent = (props) => {
const jwt = await fetchIdToken();
GetUser(user.sub, jwt).then((success) => {
if (!success) {
appInsights.trackException({
error: new Error('Error getting user'),
severityLevel: 3,
});
trackException(err, 3);
return;
}

Expand All @@ -166,10 +148,7 @@ const ProfileContent = (props) => {
}
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
});
}
});
Expand Down Expand Up @@ -236,10 +215,7 @@ const ProfileContent = (props) => {
props.setSuperDislikedRulesCount(superDislikedRules.length);
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
});
}

Expand Down Expand Up @@ -362,10 +338,7 @@ const RuleList = ({
setState(state + 1);
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
});
}

Expand Down
30 changes: 7 additions & 23 deletions src/components/reaction/reaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ import {
RemoveReaction,
} from '../../services/apiService';
import { useAuthService } from '../../services/authService';
import { ApplicationInsights } from '@microsoft/applicationinsights-web';

const appInsights = new ApplicationInsights({
config: {
connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING,
},
});
import useAppInsights from '../../hooks/useAppInsights';

const Reaction = (props) => {
const { ruleId } = props;
Expand All @@ -29,6 +23,8 @@ const Reaction = (props) => {
const { isAuthenticated, user, loginWithRedirect } = useAuth0();
const { fetchIdToken } = useAuthService();

const { trackException } = useAppInsights();

useEffect(() => {
if (isAuthenticated) {
GetReactionForUser(ruleId, user.sub)
Expand All @@ -40,10 +36,7 @@ const Reaction = (props) => {
setCurrentReactionType(success.userStatus);
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
});
} else {
setCurrentReactionType(null);
Expand All @@ -55,10 +48,7 @@ const Reaction = (props) => {
setSuperDisikesCount(success.superDislikeCount ?? 0);
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
});
}
}, [change, user]);
Expand Down Expand Up @@ -91,10 +81,7 @@ const Reaction = (props) => {
setChange(change + 1);
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
});
} else {
if (type == ReactionType.SuperLike) {
Expand All @@ -115,10 +102,7 @@ const Reaction = (props) => {
setChange(change + 1);
})
.catch((err) => {
appInsights.trackException({
error: new Error(err),
severityLevel: 3,
});
trackException(err, 3);
});
}
} else {
Expand Down
Loading

0 comments on commit 23bb717

Please sign in to comment.