Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Støtter query-parameter i Task Analytics url regler #313

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions packages/client/src/analytics/task-analytics/ta-matching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,30 @@ type Audience = Required<TaskAnalyticsSurvey>["audience"][number];
type Language = Required<TaskAnalyticsSurvey>["language"][number];
type Duration = TaskAnalyticsSurvey["duration"];

const removeTrailingSlash = (str: string) => str.replace(/\/$/, "");

const isMatchingUrl = (
url: string,
currentUrl: string,
const isMatchingCurrentLocation = (
url: URL,
match: TaskAnalyticsUrlRule["match"],
) => (match === "startsWith" ? currentUrl.startsWith(url) : currentUrl === url);
) => {
const { origin, pathname, search } = window.location;

const isMatchingUrls = (urls: TaskAnalyticsUrlRule[]) => {
const currentUrlStr = removeTrailingSlash(
`${window.location.origin}${window.location.pathname}`,
return (
url.origin === origin &&
(match === "startsWith"
? pathname.startsWith(url.pathname)
: url.pathname === pathname) &&
(!url.search || url.search === search)
);
};

const isMatchingUrls = (urls: TaskAnalyticsUrlRule[]) => {
let isMatched: boolean | null = null;
let isExcluded = false;

urls.every((urlRule) => {
const { url, match, exclude } = urlRule;
const urlToMatch = removeTrailingSlash(url);
const urlParsed = new URL(url);

if (isMatchingUrl(urlToMatch, currentUrlStr, match)) {
if (isMatchingCurrentLocation(urlParsed, match)) {
// If the url is excluded we can stop. If not, we need to continue checking the url-array, in case
// there are exclusions in the rest of the array
if (exclude) {
Expand Down
Loading