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

feat: Configure metrika origin #24

Merged
merged 1 commit into from
Nov 18, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.DS_Store
manual-test/manual-test.js
.idea
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { getRandom } from './number';
import { sendData } from './transport';
import { prepareUrl } from './url';

export { configureTransport } from './transport';

export interface LyamHitParams {
referrer?: string;
title?: string;
Expand Down
18 changes: 16 additions & 2 deletions src/transport.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import { queryStringify } from './url';
import {normalizeOrigin, queryStringify} from './url';
import type { LyamQueryParams } from './index';

interface LyamTrasportConfigParams {
metrikaOrigin: string;
}

let metrikaOrigin = 'https://mc.yandex.ru';

/**
* Конфигурирует транспорт отправки событий
* @param params
*/
export function configureTransport(params: LyamTrasportConfigParams) {
metrikaOrigin = normalizeOrigin(params.metrikaOrigin);
}

export function sendData(counterId: string, queryParams: LyamQueryParams): void {
const url = 'https://mc.yandex.ru/watch/' + counterId + '?' + queryStringify(queryParams);
const url = metrikaOrigin + '/watch/' + counterId + '?' + queryStringify(queryParams);
const hasBeacon = typeof navigator !== 'undefined' && navigator.sendBeacon;

if (!hasBeacon || !navigator.sendBeacon(url, ' ')) {
Expand Down
8 changes: 8 additions & 0 deletions src/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ const MAX_URL_LEN = 1024;
export function prepareUrl(url: string): string {
return truncate(url, MAX_URL_LEN);
}

export function normalizeOrigin(origin: string): string {
if (origin.endsWith('/')) {
return origin.slice(0, origin.length - 1)
}

return origin;
}
Loading