Skip to content

Commit b84dbb8

Browse files
authored
Merge pull request #56 from kartverket/posthog
Add posthog analytics
2 parents d30a756 + f0236dd commit b84dbb8

File tree

18 files changed

+706
-48
lines changed

18 files changed

+706
-48
lines changed

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24
1+
v24

default.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ VITE_GEOID_JWKS_URI=
1919
VITE_GEOID_KID=
2020
VITE_GEOID_N=
2121
VITE_GEOID_BAATAUTHZ_APIURL=
22+
VITE_POSTHOG_KEY=
23+
VITE_POSTHOG_HOST=https://eu.i.posthog.com

jsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"compilerOptions": {
3-
"baseUrl": "src"
3+
"baseUrl": "src",
4+
"jsx": "react-jsx",
5+
"paths": {
6+
"@/*": ["*"]
7+
}
48
},
59
"include": ["src"]
610
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
"@fortawesome/react-fontawesome": "^0.2.0",
1616
"@kartverket/geonorge-web-components": "^4.0.11",
1717
"@navikt/aksel-icons": "^8.9.0",
18+
"@posthog/react": "^1.8.2",
1819
"@uiw/react-md-editor": "^3.20.5",
1920
"classnames": "^2.2.6",
2021
"connected-react-router": "^6.5.2",
2122
"history": "^5.3.0",
2223
"js-cookie": "^3.0.1",
2324
"moment": "^2.24.0",
2425
"oidc-client-ts": "^3.3.0",
26+
"posthog-js": "^1.360.0",
2527
"r_map": "https://github.com/kartverket/r_map.git",
2628
"react": "^18.1.0",
2729
"react-app-polyfill": "^3.0.0",

public/config.template.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ window.__APP_CONFIG__ = {
1414
VITE_GEOID_JWKS_URI: "${VITE_GEOID_JWKS_URI}",
1515
VITE_GEOID_KID: "${VITE_GEOID_KID}",
1616
VITE_GEOID_N: "${VITE_GEOID_N}",
17-
VITE_GEOID_BAATAUTHZ_APIURL: "${VITE_GEOID_BAATAUTHZ_APIURL}"
17+
VITE_GEOID_BAATAUTHZ_APIURL: "${VITE_GEOID_BAATAUTHZ_APIURL}",
18+
VITE_POSTHOG_KEY: "${REACT_APP_POSTHOG_KEY}",
19+
VITE_POSTHOG_HOST: "${REACT_APP_POSTHOG_HOST}"
1820
};

src/components/partials/Buttons/ApplicationButton.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from "react";
33
import PropTypes from "prop-types";
44
import { useDispatch } from "react-redux";
55
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
6+
import { usePostHog } from "@posthog/react";
67

78
// Actions
89
import { getResource } from "@/actions/ResourceActions";
@@ -17,6 +18,7 @@ import { Button } from "@digdir/designsystemet-react";
1718

1819
const ApplicationButton = (props) => {
1920
const dispatch = useDispatch();
21+
const posthog = usePostHog();
2022

2123
const handleButtonClick = () => {
2224
const tagData = {
@@ -31,6 +33,12 @@ const ApplicationButton = (props) => {
3133
metadata: tagData
3234
})
3335
);
36+
posthog?.capture("application_link_clicked", {
37+
title: props.metadata.Title,
38+
uuid: props.metadata.Uuid,
39+
distribution_url: props.metadata.DistributionUrl || props.metadata.DownloadUrl,
40+
organization: props.metadata.Organization,
41+
});
3442
};
3543

3644
const isApplication = () => {

src/components/partials/Buttons/DownloadButton.jsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, { useEffect, useState } from "react";
44
import PropTypes from "prop-types";
55
import { useDispatch, useSelector } from "react-redux";
66
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
7+
import { usePostHog } from "@posthog/react";
78

89
// Utils
910
import userManager from "@/utils/userManager";
@@ -26,6 +27,7 @@ import { Button } from "@digdir/designsystemet-react";
2627

2728
const DownloadButton = (props) => {
2829
const dispatch = useDispatch();
30+
const posthog = usePostHog();
2931

3032
// Redux store
3133
const itemsToDownload = useSelector((state) => state.itemsToDownload);
@@ -160,11 +162,26 @@ const DownloadButton = (props) => {
160162
metadata: tagData
161163
})
162164
);
165+
posthog?.capture("download_link_clicked", {
166+
title: props.metadata.Title,
167+
uuid: props.metadata.Uuid,
168+
distribution_url: props.metadata.DistributionUrl,
169+
protocol: props.metadata.Protocol,
170+
organization: props.metadata.Organization,
171+
});
163172
};
164173

165174
const addToDownloadListAction = () => {
166175
const metadata = props.metadata;
167176
setIsLoading(true);
177+
posthog?.capture("download_added_to_basket", {
178+
title: metadata.Title,
179+
uuid: metadata.Uuid,
180+
type_name: metadata.TypeName,
181+
organization: metadata.Organization || metadata.ContactMetadata?.Organization,
182+
access_is_open_data: metadata.AccessIsOpendata,
183+
access_is_restricted: metadata.AccessIsRestricted,
184+
});
168185

169186
if (metadata.TypeName === "series_historic" || metadata.TypeName === "series_collection") {
170187
if (metadata.SerieDatasets) {

src/components/partials/Buttons/MapButton.jsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, { useEffect, useState } from "react";
33
import PropTypes from "prop-types";
44
import { useDispatch, useSelector } from "react-redux";
55
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
6+
import { usePostHog } from "@posthog/react";
67

78
// Actions
89
import { removeMapItem, addMapItem } from "@/actions/MapItemActions";
@@ -16,6 +17,7 @@ import { Button } from "@digdir/designsystemet-react";
1617

1718
const MapButton = (props) => {
1819
const dispatch = useDispatch();
20+
const posthog = usePostHog();
1921

2022
// Redux store
2123
const mapItems = useSelector((state) => state.mapItems);
@@ -120,12 +122,24 @@ const MapButton = (props) => {
120122
const addToMap = (mapItem) => {
121123
if (mapItem?.length) {
122124
dispatch(addMapItem(mapItem));
125+
posthog?.capture("map_item_added", {
126+
title: props.metadata.Title,
127+
uuid: props.metadata.Uuid,
128+
protocol: props.metadata.DistributionProtocol || props.metadata.Protocol,
129+
organization: props.metadata.Organization,
130+
});
123131
}
124132
};
125133

126134
const removeFromMap = (mapItem) => {
127135
if (mapItem?.length) {
128136
dispatch(removeMapItem(mapItem));
137+
posthog?.capture("map_item_removed", {
138+
title: props.metadata.Title,
139+
uuid: props.metadata.Uuid,
140+
protocol: props.metadata.DistributionProtocol || props.metadata.Protocol,
141+
organization: props.metadata.Organization,
142+
});
129143
}
130144
};
131145

src/components/partials/FacetFilter/Facet.jsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ import { useDispatch } from "react-redux";
55
import classNames from "classnames/bind";
66
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
77
import { Link } from "react-router-dom";
8+
import { usePostHog } from "@posthog/react";
89

910
// Reducers
1011
import { pushToDataLayer } from "@/reducers/TagManagerReducer";
1112

1213
// Helpers
13-
import { getQueryStringFromFacets } from "@/helpers/FacetFilterHelpers";
14+
import {
15+
getActiveFiltersFromSelectedFacets,
16+
getNextSelectedFacetsFromFacetToggle,
17+
getQueryStringFromFacets
18+
} from "@/helpers/FacetFilterHelpers";
1419

1520
// Components
1621
import { ErrorBoundary } from "@/components/ErrorBoundary";
@@ -20,6 +25,7 @@ import style from "@/components/partials/FacetFilter/Facet.module.scss";
2025

2126
const Facet = (props) => {
2227
const dispatch = useDispatch();
28+
const posthog = usePostHog();
2329

2430
// State
2531
const [checked, setChecked] = useState(false);
@@ -123,6 +129,8 @@ const Facet = (props) => {
123129
};
124130

125131
const handleFacetClick = () => {
132+
const action = checked ? "remove" : "add";
133+
126134
dispatch(
127135
pushToDataLayer({
128136
event: "updateSelectedFacets",
@@ -139,6 +147,20 @@ const Facet = (props) => {
139147
facet: props.facet
140148
})
141149
);
150+
151+
const selectedFacets = props?.searchData?.selectedFacets || {};
152+
const nextSelectedFacets = getNextSelectedFacetsFromFacetToggle(selectedFacets, props.facetField, props.facet, action);
153+
const activeFilters = getActiveFiltersFromSelectedFacets(nextSelectedFacets);
154+
155+
posthog?.capture("facet_filter_applied", {
156+
facet_field: props.facetField,
157+
facet_field_name: props.facetFieldNameTranslated,
158+
facet_name: props.facet.Name,
159+
facet_name_translated: props.facet.NameTranslated,
160+
facet_count: props.facet.Count,
161+
action,
162+
active_filters: activeFilters,
163+
});
142164
};
143165

144166
const renderFacet = () => {

src/components/partials/SearchResults.jsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from "react";
33
import { useDispatch } from "react-redux";
44
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
55
import { Link } from "react-router-dom";
6+
import { usePostHog } from "@posthog/react";
67

78
// Geonorge WebComponents
89
// eslint-disable-next-line no-unused-vars
@@ -23,6 +24,7 @@ import style from "@/components/partials/SearchResults.module.scss";
2324
export const SearchResults = ({ searchData, searchResultsType, viewMode }) => {
2425

2526
const dispatch = useDispatch();
27+
const posthog = usePostHog();
2628

2729
const getShowMoreLink = () => {
2830
const newOffset = searchData?.offset + 25;
@@ -84,11 +86,27 @@ export const SearchResults = ({ searchData, searchResultsType, viewMode }) => {
8486
return localStorage.getItem("urlDownloadCsv");
8587
};
8688

89+
const handleShowMoreClick = () => {
90+
posthog?.capture("show_more_results_clicked", {
91+
results_type: searchResultsType,
92+
current_offset: searchData?.offset,
93+
search_string: searchData?.searchString,
94+
});
95+
};
96+
97+
const handleCsvDownloadClick = () => {
98+
posthog?.capture("search_results_csv_clicked", {
99+
results_type: searchResultsType,
100+
search_string: searchData?.searchString,
101+
csv_url: downloadAsCsvUrl(),
102+
});
103+
};
104+
87105
const renderShowMoreLink = () => {
88106
return (
89107
<div className={style.morecontainer}>
90108
<gn-button color="default">
91-
<Link to={{ search: getShowMoreLink() }} replace className={style.morebtn}>
109+
<Link to={{ search: getShowMoreLink() }} replace className={style.morebtn} onClick={handleShowMoreClick}>
92110
<span>{dispatch(getResource("ShowMoreResults", "Vis flere"))}</span>
93111
<FontAwesomeIcon icon={"angle-down"} key="icon" />
94112
</Link>
@@ -109,7 +127,7 @@ export const SearchResults = ({ searchData, searchResultsType, viewMode }) => {
109127
<div className={style.searchResultContainer}>
110128
{renderMetadataSearchResults()}
111129
<div className={style.downloadcsv}>
112-
<a href={downloadAsCsvUrl()}>
130+
<a href={downloadAsCsvUrl()} onClick={handleCsvDownloadClick}>
113131
{dispatch(getResource("SaveResultsAsCSV", "Lagre listen som CSV"))}
114132
</a>
115133
</div>

0 commit comments

Comments
 (0)