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

new metrics and dimensions for sdk init #175

Merged
merged 2 commits into from
Feb 2, 2024
Merged
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
131 changes: 98 additions & 33 deletions src/tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,53 @@
getLocale,
getSDKScript,
getSDKIntegrationSource,
getPageType,
getPageType as getSDKPageType,
getClientToken,
getUserIDToken,
getSDKToken,
} from "./script";
import { getSessionID } from "./session";
import { getLogger } from "./logger";
import { isPayPalDomain } from "./domains";

let sdkInitTime;

const getTokenType = (): string => {
if (getClientToken()) {
return "client-token";
}

if (getUserIDToken()) {
return "user-id-token";
}

if (getSDKToken()) {
return "sdk-token";
}

return "none";
};

Check warning on line 55 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L42-L55

Added lines #L42 - L55 were not covered by tests

const getIntegrationSource = (): string => {
const integrationSource = getSDKIntegrationSource();

if (integrationSource) {
return integrationSource;

Check warning on line 61 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L61

Added line #L61 was not covered by tests
} else {
return "none";
}
};

const getPageType = (): string => {
const pageType = getSDKPageType();

if (pageType) {
return pageType;

Check warning on line 71 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L71

Added line #L71 was not covered by tests
} else {
return "none";
}
};

export function getSDKInitTime(): number {
if (typeof sdkInitTime === "undefined") {
throw new TypeError(`SDK not initialized`);
Expand All @@ -45,6 +84,13 @@

export function setupLogger() {
const logger = getLogger();
const pageType = getPageType();
const integrationSource = getIntegrationSource();
const version = getVersion();
const userAction = getCommit()
? FPTI_USER_ACTION.COMMIT
: FPTI_USER_ACTION.CONTINUE;

sdkInitTime = Date.now();

logger.addPayloadBuilder(() => {
Expand All @@ -69,15 +115,13 @@
[FPTI_KEY.LOCALE]: `${lang}_${country}`,
[FPTI_KEY.INTEGRATION_IDENTIFIER]: getClientID(),
[FPTI_KEY.PARTNER_ATTRIBUTION_ID]: getPartnerAttributionID(),
[FPTI_KEY.PAGE_TYPE]: getPageType(),
[FPTI_KEY.PAGE_TYPE]: pageType,

Check warning on line 118 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L118

Added line #L118 was not covered by tests
[FPTI_KEY.SDK_NAME]: FPTI_SDK_NAME.PAYMENTS_SDK,
[FPTI_KEY.SDK_VERSION]: getVersion(),
[FPTI_KEY.SDK_VERSION]: version,

Check warning on line 120 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L120

Added line #L120 was not covered by tests
[FPTI_KEY.USER_AGENT]: window.navigator && window.navigator.userAgent,
[FPTI_KEY.USER_ACTION]: getCommit()
? FPTI_USER_ACTION.COMMIT
: FPTI_USER_ACTION.CONTINUE,
[FPTI_KEY.USER_ACTION]: userAction,

Check warning on line 122 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L122

Added line #L122 was not covered by tests
[FPTI_KEY.CONTEXT_CORRID]: getCorrelationID(),
[FPTI_KEY.SDK_INTEGRATION_SOURCE]: getSDKIntegrationSource(),
[FPTI_KEY.SDK_INTEGRATION_SOURCE]: integrationSource,

Check warning on line 124 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L124

Added line #L124 was not covered by tests
};
});

Expand All @@ -98,14 +142,14 @@
waitForWindowReady().then(() => {
const sdkScript = getSDKScript();
const loadTime = getResourceLoadTime(sdkScript.src);
let cache;
let cacheType;

if (loadTime === 0) {
cache = "sdk_client_cache_hit";
cacheType = "sdk_client_cache_hit";

Check warning on line 148 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L148

Added line #L148 was not covered by tests
} else if (typeof loadTime === "number") {
cache = "sdk_client_cache_miss";
cacheType = "sdk_client_cache_miss";

Check warning on line 150 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L150

Added line #L150 was not covered by tests
} else {
cache = "sdk_client_cache_unknown";
cacheType = "sdk_client_cache_unknown";
}

// Exclude apps that use the JS SDK and are hosted directly on www.paypal.com. Ex:
Expand All @@ -114,28 +158,49 @@
const isLoadedInFrame = isPayPalDomain() && window.xprops;
const sdkLoadTime = typeof loadTime === "number" ? loadTime : undefined;

logger
.info(`setup_${getEnv()}`)
.info(`setup_${getEnv()}_${getVersion().replace(/\./g, "_")}`)
.info(
`sdk_${isLoadedInFrame ? "paypal" : "non_paypal"}_domain_script_uid_${
sdkScript.hasAttribute(ATTRIBUTES.UID) ? "present" : "missing"
}`
)
.info(cache, { sdkLoadTime })
// $FlowFixMe beaver-logger types need to be updated
.metric({
metricNamespace: "pp.app.sdk.paypal_js_v5.sdk_load_time.gauge",
metricEventName: "unused",
metricValue: sdkLoadTime,
dimensions: { cacheType: cache, components: getComponents().join(",") },
})
.track({
[FPTI_KEY.TRANSITION]: "process_js_sdk_init_client",
[FPTI_KEY.SDK_LOAD_TIME]: sdkLoadTime,
[FPTI_KEY.SDK_CACHE]: cache,
})
.flush();
logger.info(
`sdk_${isLoadedInFrame ? "paypal" : "non_paypal"}_domain_script_uid_${
sdkScript.hasAttribute(ATTRIBUTES.UID) ? "present" : "missing"
}`
);

if (loadTime) {
logger
// We can not send gauge metrics to our logger backend currently
// once we have that ability, we should uncomment this gauge metric
// .metric({
// metricNamespace: "pp.app.paypal_sdk.init.gauge",
// metricType: "gauge",
// metricEventName: "load_performance",
// metricValue: sdkLoadTime,
// dimensions: {
// cacheType,
// version,
// components: getComponents().join(","),
// isPayPalDomain: isLoadedInFrame,
// token: getTokenType(),
// },
// })
// $FlowIssue
.metric({
metricNamespace: "pp.app.paypal_sdk.init.count",
metricEventName: "load",
dimensions: {
integrationSource,
pageType,
userAction,
version,
components: getComponents().join(","),
isPayPalDomain: isLoadedInFrame,
token: getTokenType(),
},
})
.track({
[FPTI_KEY.TRANSITION]: "process_js_sdk_init_client",
[FPTI_KEY.SDK_LOAD_TIME]: sdkLoadTime,
[FPTI_KEY.SDK_CACHE]: cacheType,
});
}

Check warning on line 203 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L168-L203

Added lines #L168 - L203 were not covered by tests

if (isIEIntranet()) {
logger.warn("ie_intranet_mode");
Expand Down
Loading