Skip to content

Commit

Permalink
feat: allow overriding of api endpoint & capture dataset (#262)
Browse files Browse the repository at this point in the history
* feat: allow overriding of api endpoint & capture dataset

* feat: address review comments
  • Loading branch information
vijaye-statsig authored Jul 12, 2024
1 parent fe477b4 commit 5901129
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
16 changes: 10 additions & 6 deletions packages/combo/src/AutoInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
_getDocumentSafe,
_getWindowSafe,
} from '@statsig/client-core';
import { StatsigClient } from '@statsig/js-client';
import { StatsigClient, StatsigOptions } from '@statsig/js-client';

type InitArgs = {
sdkKey: string;
Expand Down Expand Up @@ -49,35 +49,39 @@ export abstract class AutoInit {
try {
const win = _getWindowSafe();
const doc = _getDocumentSafe();

if (!win || !doc || !doc.currentScript) {
return;
}

const srcUrl = doc.currentScript.getAttribute('src');
const baseUrl = win.location?.href;

if (!srcUrl || !baseUrl) {
return;
}

const url = new URL(srcUrl, baseUrl);
const params = url.searchParams;
const sdkKey = _getParam('sdkKey', params) ?? _getParam('apiKey', params);

if (!sdkKey) {
return;
}

const options: StatsigOptions = {};
const proxy = _getParam('proxy', params);
if (proxy) {
options['networkConfig'] = {
api: proxy,
};
}

const current: unknown = __STATSIG__?.instances?.[sdkKey];
let client: StatsigClient | null = null;

if (current instanceof StatsigClient) {
client = current;
}

if (!client) {
client = new StatsigClient(sdkKey, _constructUser());
client = new StatsigClient(sdkKey, _constructUser(), options);
client.initializeAsync().catch((err) => {
Log.error(err);
});
Expand Down
19 changes: 19 additions & 0 deletions packages/web-analytics/src/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { _getCurrentPageUrlSafe, _getWindowSafe } from '@statsig/client-core';

export function _gatherDatasetProperties(el: Element): Record<string, string> {
const dataset = {} as Record<string, string>;
if (!el) {
return dataset;
}
const attr = (el as HTMLElement)?.dataset;
if (!attr) {
return dataset;
}

for (const key in attr) {
dataset[`data-${key}`] = attr[key] || '';
}

return dataset;
}

export function _gatherEventData(target: Element): {
value: string;
metadata: Record<string, string | null>;
Expand Down Expand Up @@ -31,6 +48,8 @@ export function _gatherEventData(target: Element): {

if (tagName === 'button' || anchor) {
metadata['content'] = (target.textContent || '').trim();
const dataset = _gatherDatasetProperties(anchor || target);
Object.assign(metadata, dataset);
}

return { value, metadata };
Expand Down
3 changes: 2 additions & 1 deletion tools/scripts/minifier-name-cache.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@
"$_downloadConfigSpecsUrl": "cn",
"$_getSpecs": "dn",
"$_evaluator": "hn",
"$_getOutputs": "vn"
"$_getOutputs": "vn",
"$_gatherDatasetProperties": "fn"
}
}
}

0 comments on commit 5901129

Please sign in to comment.