Skip to content

Commit

Permalink
feat: Matomo track events (search, lang)
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Oct 9, 2024
1 parent efd3676 commit 38ab3e7
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Support `.env`
- Simplify time interval CQP covering whole days [#379](https://github.com/spraakbanken/korp-frontend/issues/379)
- Track some events with Matomo: search, language switch

### Changed

Expand Down
2 changes: 2 additions & 0 deletions app/scripts/components/advanced-search.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @format */
import angular from "angular"
import { html } from "@/util"
import { matomoSend } from "@/matomo"
import "@/services/compare-searches"
import "@/components/search-submit"

Expand Down Expand Up @@ -80,6 +81,7 @@ angular.module("korpApp").component("advancedSearch", {
$location.search("within", null)
$location.search("in_order", $ctrl.freeOrder ? false : null)
$timeout(() => $location.search("search", `cqp|${$ctrl.cqp}`), 0)
matomoSend("trackEvent", "Search", "Submit search", "Advanced")
}

$ctrl.onSearchSave = (name) => {
Expand Down
2 changes: 2 additions & 0 deletions app/scripts/components/extended/extended-parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import _ from "lodash"
import settings from "@/settings"
import { expandOperators } from "@/cqp_parser/cqp"
import { html } from "@/util"
import { matomoSend } from "@/matomo"
import "@/services/searches"
import "@/components/extended/tokens"

Expand Down Expand Up @@ -159,6 +160,7 @@ angular.module("korpApp").component("extendedParallel", {
$location.search("search", `cqp|${onCQPChange()}`)
$location.search("page", null)
}, 0)
matomoSend("trackEvent", "Search", "Submit search", "Extended")
}

ctrl.keydown = function ($event) {
Expand Down
2 changes: 2 additions & 0 deletions app/scripts/components/extended/extended-standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import statemachine from "@/statemachine"
import settings from "@/settings"
import { expandOperators, mergeCqpExprs, parse, stringify, supportsInOrder } from "@/cqp_parser/cqp"
import { html } from "@/util"
import { matomoSend } from "@/matomo"
import "@/services/compare-searches"
import "@/components/extended/tokens"
import "@/components/search-submit"
Expand Down Expand Up @@ -85,6 +86,7 @@ angular.module("korpApp").component("extendedStandard", {
})

ctrl.onSearch = () => {
matomoSend("trackEvent", "Search", "Submit search", "Extended")
triggerSearch()
}

Expand Down
4 changes: 4 additions & 0 deletions app/scripts/components/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { collatorSort, html } from "@/util"
import "@/services/utils"
import "@/components/corpus-chooser/corpus-chooser"
import "@/components/radio-list"
import { matomoSend } from "@/matomo"

angular.module("korpApp").component("header", {
template: html`
Expand Down Expand Up @@ -152,6 +153,9 @@ angular.module("korpApp").component("header", {
$rootScope["lang"] = $scope.lang
// Set url param if different from default.
$location.search("lang", $scope.lang !== settings["default_language"] ? $scope.lang : null)

if (!oldVal) matomoSend("trackEvent", "UI", "Locale init", $scope.lang)
else matomoSend("trackEvent", "UI", "Locale switch", $scope.lang)
})

$ctrl.citeClick = () => {
Expand Down
2 changes: 2 additions & 0 deletions app/scripts/components/simple-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import statemachine from "@/statemachine"
import settings from "@/settings"
import { expandOperators, mergeCqpExprs, parse, stringify, supportsInOrder } from "@/cqp_parser/cqp"
import { html, regescape, saldoToHtml, unregescape } from "@/util"
import { matomoSend } from "@/matomo"
import "@/services/compare-searches"
import "@/services/lexicons"
import "@/services/searches"
Expand Down Expand Up @@ -132,6 +133,7 @@ angular.module("korpApp").component("simpleSearch", {
}
$location.search("page", null)
}, 0)
matomoSend("trackEvent", "Search", "Submit search", "Simple")
}

ctrl.getCQP = function () {
Expand Down
23 changes: 23 additions & 0 deletions app/scripts/matomo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,26 @@ if (matomoSettings.url && matomoSettings.site) {
s.parentNode!.insertBefore(g, s)
})()
}

/**
* Send a command to Matomo.
*
* Does nothing if Matomo is not configured.
*/
export function matomoSend<P extends keyof Matomo>(command: P, ...args: Parameters<Matomo[P]>) {
window._paq = window._paq || []
window._paq.push([command, ...args])
}

/**
* This type describes available Matomo commands.
*
* Extend as needed.
* Fully described on https://developer.matomo.org/api-reference/tracking-javascript
*/
export type Matomo = {
trackEvent: (category: string, action: string, name?: string, value?: number) => void
trackLink: (url: string, linkType: string) => void
trackPageView: (customTitle?: string) => void
trackSiteSearch: (keyword: string, category?: string, resultsCount?: number) => void
}

0 comments on commit 38ab3e7

Please sign in to comment.