Skip to content

Commit d9a7949

Browse files
committed
Add initial google event tracking for active query items
1 parent 234d7fb commit d9a7949

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

web/src/use/useGtag.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { getCurrentInstance } from '@vue/composition-api';
2+
3+
/**
4+
* Custom hook to access Google Analytics gtag instance.
5+
* returns $gtag instance if available, otherwise undefined.
6+
* In development, this will return undefined.
7+
*/
8+
export default function useGtag() {
9+
const instance = getCurrentInstance();
10+
const gtag = instance?.proxy?.$gtag;
11+
return gtag;
12+
}

web/src/views/Search/SearchSidebar.vue

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import FacetedSearch, { SearchFacet } from '@/components/FacetedSearch.vue';
1818
import {
1919
stateRefs, removeConditions, setConditions, toggleConditions,
2020
} from '@/store';
21+
import useGtag from '@/use/useGtag';
2122
2223
/**
2324
* Sidebar has a fixed list of facets, possibly from different tables.
@@ -144,6 +145,7 @@ export default defineComponent({
144145
const filterText = ref('');
145146
const textSearchResults = ref([] as Condition[]);
146147
const dbSummary = ref({} as DatabaseSummaryResponse);
148+
const gtag = useGtag();
147149
const biosampleDescription = computed(() => {
148150
const { schemaName } = types.biosample;
149151
if (schemaName !== undefined) {
@@ -183,6 +185,30 @@ export default defineComponent({
183185
}
184186
watch(filterText, updateSearch);
185187
188+
function trackFilterConditions(val: Condition[], oldVal: Condition[]) {
189+
if (!gtag) {
190+
console.warn('Google Analytics is not available. This is expected in development mode.');
191+
return;
192+
}
193+
// On initial load, track each filter condition that exists
194+
// Otherwise, track the last filter condition added or updated
195+
if (oldVal.length === 0 && val.length > 0) {
196+
val.forEach((condition) => {
197+
gtag.event('Add filter', {
198+
event_label: condition.field,
199+
value: condition.value,
200+
});
201+
});
202+
} else if (val.length > oldVal.length || val.length === oldVal.length) {
203+
gtag.event('Add filter', {
204+
event_label: val[val.length - 1].field,
205+
value: val[val.length - 1].value,
206+
});
207+
}
208+
}
209+
210+
watch(stateRefs.conditions, trackFilterConditions);
211+
186212
return {
187213
/* data */
188214
biosampleDescription,

0 commit comments

Comments
 (0)