refactor: rename and add init tracker function

This commit is contained in:
moonrailgun 2024-04-12 00:47:06 +08:00
parent 699aedc272
commit cdcd6e2284
7 changed files with 88 additions and 5 deletions

View File

@ -8,7 +8,12 @@
"prepare": "tsc", "prepare": "tsc",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"keywords": ["tianji"], "keywords": [
"tianji"
],
"author": "moonrailgun <moonrailgun@gmail.com>", "author": "moonrailgun <moonrailgun@gmail.com>",
"license": "MIT" "license": "MIT",
"dependencies": {
"load-script": "^2.0.0"
}
} }

View File

@ -0,0 +1 @@
export * from './tracker';

View File

@ -0,0 +1,70 @@
import loadScript from 'load-script';
interface InjectTrackerOptions {
/**
* your tianji application website
* @example
* https://tianji.example.com
*/
url: string;
/**
* Website id, you can get it from your tianji dashboard
*/
websiteId: string;
/**
* If you modify your tracker name with
*/
customTrackerName?: string;
/**
* is auto track route change and dom
*/
autoTrack?: boolean;
/**
* Whitelist domains, default will report all website
* @example
* example.com, www.example.com
*/
domains?: string[];
disableTrack?: boolean;
}
export async function initTianjiTracker(options: InjectTrackerOptions) {
const trackerName = options.customTrackerName ?? 'tracker.js';
const attrs: Record<string, string> = {
'data-website-id': options.websiteId,
};
if (options.autoTrack === false) {
attrs['data-auto-track'] = 'false';
}
if (Array.isArray(options.domains)) {
attrs['data-domains'] = options.domains.join(',');
}
if (options.disableTrack === true) {
attrs['data-do-not-track'] = 'true';
}
return new Promise<void>((resolve, reject) => {
loadScript(
`${options.url}/${trackerName}`,
{
async: true,
attrs,
},
(err, script) => {
if (err) {
reject(err);
return;
}
resolve();
}
);
});
}

View File

@ -1,3 +0,0 @@
export function main() {
console.log('Hello World');
}

View File

@ -61,6 +61,12 @@ importers:
specifier: ^5.0.12 specifier: ^5.0.12
version: 5.0.12(@types/node@18.17.12)(less@4.2.0) version: 5.0.12(@types/node@18.17.12)(less@4.2.0)
packages/client-sdk:
dependencies:
load-script:
specifier: ^2.0.0
version: 2.0.0
src/client: src/client:
dependencies: dependencies:
'@ant-design/charts': '@ant-design/charts':
@ -17182,6 +17188,10 @@ packages:
engines: {node: '>=4'} engines: {node: '>=4'}
dev: false dev: false
/load-script@2.0.0:
resolution: {integrity: sha512-km6cyoPW4rM22JMGb+SHUKPMZVDpUaMpMAKrv8UHWllIxc/qjgMGHD91nY+5hM+/NFs310OZ2pqQeJKs7HqWPA==}
dev: false
/loader-runner@4.3.0: /loader-runner@4.3.0:
resolution: {integrity: sha512-3R/1M+yS3j5ou80Me59j7F9IMs4PXs3VqRrm0TU3AbKPxlmpoY1TNscJV/oGJXo8qCatFGTfDbY6W6ipGOYXfg==} resolution: {integrity: sha512-3R/1M+yS3j5ou80Me59j7F9IMs4PXs3VqRrm0TU3AbKPxlmpoY1TNscJV/oGJXo8qCatFGTfDbY6W6ipGOYXfg==}
engines: {node: '>=6.11.5'} engines: {node: '>=6.11.5'}