Skip to content

Commit

Permalink
chore: add project id source (#8983)
Browse files Browse the repository at this point in the history
Add `projectIDSource` to analytics event.
  • Loading branch information
denolfe authored Nov 1, 2024
1 parent 10d5a8f commit 59ff8c1
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions packages/payload/src/utilities/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type BaseEvent = {
nodeVersion: string
payloadVersion: string
projectID: string
projectIDSource: 'cwd' | 'git' | 'packageJSON' | 'serverURL'
uploadAdapters: string[]
}

Expand All @@ -49,14 +50,16 @@ export const sendEvent = async ({ event, payload }: Args): Promise<void> => {

// Only generate the base event once
if (!baseEvent) {
const { projectID, source: projectIDSource } = getProjectID(payload, packageJSON)
baseEvent = {
ciName: ciInfo.isCI ? ciInfo.name : null,
envID: getEnvID(),
isCI: ciInfo.isCI,
nodeEnv: process.env.NODE_ENV || 'development',
nodeVersion: process.version,
payloadVersion: getPayloadVersion(packageJSON),
projectID: getProjectID(payload, packageJSON),
projectID,
projectIDSource,
...getLocalizationInfo(payload),
dbAdapter: payload.db.name,
emailAdapter: payload.email?.name || null,
Expand Down Expand Up @@ -104,13 +107,27 @@ const getEnvID = (): string => {
return generated
}

const getProjectID = (payload: Payload, packageJSON: PackageJSON): string => {
const projectID =
getGitID(payload) ||
getPackageJSONID(payload, packageJSON) ||
payload.config.serverURL ||
process.cwd()
return oneWayHash(projectID, payload.secret)
const getProjectID = (
payload: Payload,
packageJSON: PackageJSON,
): { projectID: string; source: BaseEvent['projectIDSource'] } => {
const gitID = getGitID(payload)
if (gitID) {
return { projectID: oneWayHash(gitID, payload.secret), source: 'git' }
}

const packageJSONID = getPackageJSONID(payload, packageJSON)
if (packageJSONID) {
return { projectID: oneWayHash(packageJSONID, payload.secret), source: 'packageJSON' }
}

const serverURL = payload.config.serverURL
if (serverURL) {
return { projectID: oneWayHash(serverURL, payload.secret), source: 'serverURL' }
}

const cwd = process.cwd()
return { projectID: oneWayHash(cwd, payload.secret), source: 'cwd' }
}

const getGitID = (payload: Payload) => {
Expand Down

0 comments on commit 59ff8c1

Please sign in to comment.