@@ -18,6 +18,7 @@ import FacetedSearch, { SearchFacet } from '@/components/FacetedSearch.vue';
18
18
import {
19
19
stateRefs , removeConditions , setConditions , toggleConditions ,
20
20
} from ' @/store' ;
21
+ import useGtag from ' @/use/useGtag' ;
21
22
22
23
/**
23
24
* Sidebar has a fixed list of facets, possibly from different tables.
@@ -144,6 +145,7 @@ export default defineComponent({
144
145
const filterText = ref (' ' );
145
146
const textSearchResults = ref ([] as Condition []);
146
147
const dbSummary = ref ({} as DatabaseSummaryResponse );
148
+ const gtag = useGtag ();
147
149
const biosampleDescription = computed (() => {
148
150
const { schemaName } = types .biosample ;
149
151
if (schemaName !== undefined ) {
@@ -183,6 +185,30 @@ export default defineComponent({
183
185
}
184
186
watch (filterText , updateSearch );
185
187
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
+
186
212
return {
187
213
/* data */
188
214
biosampleDescription ,
0 commit comments