Skip to content

Commit

Permalink
Require siteId parameter in MatomoTracker constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
AryanJ-NYC committed Sep 5, 2020
1 parent ccbf850 commit 748ec7b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Prefix the change with one of these keywords:
- _Fixed_: for any bug fixes.
- _Security_: in case of vulnerabilities.

## [Unreleased]

- [BREAKING] Changed: The `siteId` option in the `MatomoTracker` constructor is now required.

## [0.2.1]

Expand Down
2 changes: 1 addition & 1 deletion packages/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import MatomoTracker from '@datapunt/matomo-tracker-js'

const tracker = new MatomoTracker({
urlBase: 'https://LINK.TO.DOMAIN',
siteId: 3, // optional, default value: `1`
siteId: 3,
userId: 'UID76903202', // optional, default value: `undefined`.
trackerUrl: 'https://LINK.TO.DOMAIN/tracking.php', // optional, default value: `${urlBase}matomo.php`
srcUrl: 'https://LINK.TO.DOMAIN/tracking.js', // optional, default value: `${urlBase}matomo.js`
Expand Down
12 changes: 12 additions & 0 deletions packages/js/src/MatomoTracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ import MatomoTracker from './MatomoTracker'
const URL_BASE = 'https://example.com'

describe('MatomoTracker', () => {
it('throws an error if no urlBase is passed in options', () => {
// eslint-disable-next-line
// @ts-expect-error
expect(() => new MatomoTracker({ siteId: 1 })).toThrowError()
})

it('throws an error if no siteId is passed in options', () => {
// eslint-disable-next-line
// @ts-expect-error
expect(() => new MatomoTracker({ urlBase: URL_BASE })).toThrowError()
})

describe('pushInstruction', () => {
it('should push the instruction', () => {
const matomo = new MatomoTracker({
Expand Down
11 changes: 6 additions & 5 deletions packages/js/src/MatomoTracker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultOptions, TRACK_TYPES } from './constants'
import { TRACK_TYPES } from './constants'
import {
AddEcommerceItemParams,
CustomDimension,
Expand All @@ -16,13 +16,14 @@ class MatomoTracker {
mutationObserver?: MutationObserver

constructor(userOptions: UserOptions) {
const options = { ...defaultOptions, ...userOptions }

if (!options.urlBase) {
if (!userOptions.urlBase) {
throw new Error('Matomo urlBase is required.')
}
if (!userOptions.siteId) {
throw new Error('Matomo siteId is required.')
}

this.initialize(options)
this.initialize(userOptions)
}

private initialize({
Expand Down
6 changes: 1 addition & 5 deletions packages/js/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
export const defaultOptions = {
urlBase: null,
siteId: 1,
}

// eslint-disable-next-line import/prefer-default-export
export const TRACK_TYPES = {
TRACK_EVENT: 'trackEvent',
TRACK_LINK: 'trackLink',
Expand Down
2 changes: 1 addition & 1 deletion packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { MatomoProvider, createInstance } from '@datapunt/matomo-tracker-react'

const instance = createInstance({
urlBase: 'https://LINK.TO.DOMAIN',
siteId: 3, // optional, default value: `1`
siteId: 3,
userId: 'UID76903202', // optional, default value: `undefined`.
trackerUrl: 'https://LINK.TO.DOMAIN/tracking.php', // optional, default value: `${urlBase}matomo.php`
srcUrl: 'https://LINK.TO.DOMAIN/tracking.js', // optional, default value: `${urlBase}matomo.js`
Expand Down

0 comments on commit 748ec7b

Please sign in to comment.