Skip to content

Commit

Permalink
new metrics and dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
wsbrunson committed Dec 8, 2023
1 parent df528dc commit fb85b31
Showing 1 changed file with 95 additions and 33 deletions.
128 changes: 95 additions & 33 deletions src/tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,53 @@ import {
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";

Check warning on line 43 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L42-L43

Added lines #L42 - L43 were not covered by tests
}

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

Check warning on line 47 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L46-L47

Added lines #L46 - L47 were not covered by tests
}

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

Check warning on line 51 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L50-L51

Added lines #L50 - L51 were not covered by tests
}

return "none";

Check warning on line 54 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L54

Added line #L54 was not covered by tests
};

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

if (integrationSource) {
return integrationSource;
} else {
return "none";
}
};

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

if (pageType) {
return pageType;
} else {
return "none";
}
};

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

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();

Expand All @@ -70,15 +115,13 @@ export function setupLogger() {
[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,
[FPTI_KEY.SDK_NAME]: FPTI_SDK_NAME.PAYMENTS_SDK,
[FPTI_KEY.SDK_VERSION]: getVersion(),
[FPTI_KEY.SDK_VERSION]: version,
[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,
[FPTI_KEY.CONTEXT_CORRID]: getCorrelationID(),
[FPTI_KEY.SDK_INTEGRATION_SOURCE]: getSDKIntegrationSource(),
[FPTI_KEY.SDK_INTEGRATION_SOURCE]: integrationSource,
};
});

Expand All @@ -99,14 +142,14 @@ export function setupLogger() {
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 @@ -115,28 +158,47 @@ export function setupLogger() {
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

Check warning on line 168 in src/tracking.js

View check run for this annotation

Codecov / codecov/patch

src/tracking.js#L168

Added line #L168 was not covered by tests
// $FlowFixMe beaver-logger types need to be updated
.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(),
},
})
.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,
});
}

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

0 comments on commit fb85b31

Please sign in to comment.