diff --git a/packages/sdk/.gitignore b/packages/client-sdk/.gitignore similarity index 100% rename from packages/sdk/.gitignore rename to packages/client-sdk/.gitignore diff --git a/packages/sdk/package.json b/packages/client-sdk/package.json similarity index 72% rename from packages/sdk/package.json rename to packages/client-sdk/package.json index 69f1731..fede1a9 100644 --- a/packages/sdk/package.json +++ b/packages/client-sdk/package.json @@ -8,7 +8,12 @@ "prepare": "tsc", "test": "echo \"Error: no test specified\" && exit 1" }, - "keywords": ["tianji"], + "keywords": [ + "tianji" + ], "author": "moonrailgun ", - "license": "MIT" + "license": "MIT", + "dependencies": { + "load-script": "^2.0.0" + } } diff --git a/packages/client-sdk/src/index.ts b/packages/client-sdk/src/index.ts new file mode 100644 index 0000000..c6df020 --- /dev/null +++ b/packages/client-sdk/src/index.ts @@ -0,0 +1 @@ +export * from './tracker'; diff --git a/packages/client-sdk/src/tracker.ts b/packages/client-sdk/src/tracker.ts new file mode 100644 index 0000000..27772f5 --- /dev/null +++ b/packages/client-sdk/src/tracker.ts @@ -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 = { + '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((resolve, reject) => { + loadScript( + `${options.url}/${trackerName}`, + { + async: true, + attrs, + }, + (err, script) => { + if (err) { + reject(err); + return; + } + + resolve(); + } + ); + }); +} diff --git a/packages/sdk/tsconfig.json b/packages/client-sdk/tsconfig.json similarity index 100% rename from packages/sdk/tsconfig.json rename to packages/client-sdk/tsconfig.json diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts deleted file mode 100644 index feef3fb..0000000 --- a/packages/sdk/src/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function main() { - console.log('Hello World'); -} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7a4a711..1da5b6e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -61,6 +61,12 @@ importers: specifier: ^5.0.12 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: dependencies: '@ant-design/charts': @@ -17182,6 +17188,10 @@ packages: engines: {node: '>=4'} dev: false + /load-script@2.0.0: + resolution: {integrity: sha512-km6cyoPW4rM22JMGb+SHUKPMZVDpUaMpMAKrv8UHWllIxc/qjgMGHD91nY+5hM+/NFs310OZ2pqQeJKs7HqWPA==} + dev: false + /loader-runner@4.3.0: resolution: {integrity: sha512-3R/1M+yS3j5ou80Me59j7F9IMs4PXs3VqRrm0TU3AbKPxlmpoY1TNscJV/oGJXo8qCatFGTfDbY6W6ipGOYXfg==} engines: {node: '>=6.11.5'}