Skip to content
Merged
Show file tree
Hide file tree
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
36 changes: 33 additions & 3 deletions composables/useUTM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,45 @@ export const useUTM = (baseUrl: string, content: string) => {
/**
* Generates a URL with UTM tracking parameters
*
* @returns The URL with UTM parameters
* @returns {string} The URL with UTM parameters
*/
const getUrl = (): string => {
const url = new URL(baseUrl);
let url: URL;

/* Add UTM parameters */
try {
url = new URL(baseUrl);
} catch (error) {
return baseUrl;
}

/* Default UTM parameters */
url.searchParams.set('utm_source', 'hawk-tracker.ru');
url.searchParams.set('utm_content', content);

/* If current page has utm_* params, override defaults with those */
if (process.client && typeof window !== 'undefined') {
const currentSearchParams = new URLSearchParams(window.location.search || '');

/**
* If utm_content is overridden, we will add original button content to utm_place
*/
let isUtmContentOverridden = false;

currentSearchParams.forEach((value, key) => {
if (key.toLowerCase().startsWith('utm_')) {
if (key.toLowerCase() === 'utm_content') {
isUtmContentOverridden = true;
}

url.searchParams.set(key, value);
}
});

if (isUtmContentOverridden && !url.searchParams.has('utm_place')) {
url.searchParams.set('utm_place', content);
}
}

return url.toString();
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hawk-yard",
"version": "2.0.0",
"version": "2.0.1",
"private": true,
"engines": {
"node": ">=20.11.0",
Expand Down