Skip to content

Commit

Permalink
chore(application-generic): Stop logging Segment and Mixpanel invocat…
Browse files Browse the repository at this point in the history
…ions (#6664)
  • Loading branch information
SokratisVidros authored Oct 10, 2024
1 parent e10fb36 commit d5b284d
Showing 1 changed file with 94 additions and 103 deletions.
197 changes: 94 additions & 103 deletions libs/application-generic/src/services/analytics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,112 +41,111 @@ export class AnalyticsService {
organization: IOrganizationEntity,
user: IUser,
) {
if (this.segmentEnabled) {
const traits: Record<string, string | string[]> = {
_organization: organizationId,
id: organizationId,
name: organization.name,
createdAt: organization.createdAt,
domain: organization.domain || user.email?.split('@')[1],
};

if (organization.productUseCases) {
const productUseCases: string[] = [];

for (const key in organization.productUseCases) {
if (organization.productUseCases[key]) {
productUseCases.push(key);
}
if (!this.segmentEnabled) {
return;
}

const traits: Record<string, string | string[]> = {
_organization: organizationId,
id: organizationId,
name: organization.name,
createdAt: organization.createdAt,
domain: organization.domain || user.email?.split('@')[1],
};

if (organization.productUseCases) {
const productUseCases: string[] = [];

for (const key in organization.productUseCases) {
if (organization.productUseCases[key]) {
productUseCases.push(key);
}
traits.productUseCases = productUseCases;
}

this.segment.group({
userId: user._id as any,
groupId: organizationId,
traits,
});
traits.productUseCases = productUseCases;
}

this.segment.group({
userId: user._id as any,
groupId: organizationId,
traits,
});
}

alias(distinctId: string, userId: string) {
if (this.segmentEnabled) {
this.segment.alias({
previousId: distinctId,
userId,
});
if (!this.segmentEnabled) {
return;
}

this.segment.alias({
previousId: distinctId,
userId,
});
}

upsertUser(user: IUser, distinctId: string) {
if (this.segmentEnabled) {
const githubToken = (user as any).tokens?.find(
(token) => token.provider === 'github',
);

this.segment.identify({
userId: distinctId,
traits: {
firstName: user.firstName,
lastName: user.lastName,
name: `${user.firstName || ''} ${user.lastName || ''}`.trim(),
email: user.email,
avatar: user.profilePicture,
createdAt: user.createdAt,
// For segment auto mapping
created: user.createdAt,
githubProfile: githubToken?.username,
},
});
if (!this.segmentEnabled) {
return;
}

const githubToken = (user as any).tokens?.find(
(token) => token.provider === 'github',
);

this.segment.identify({
userId: distinctId,
traits: {
firstName: user.firstName,
lastName: user.lastName,
name: `${user.firstName || ''} ${user.lastName || ''}`.trim(),
email: user.email,
avatar: user.profilePicture,
createdAt: user.createdAt,
// For segment auto mapping
created: user.createdAt,
githubProfile: githubToken?.username,
},
});
}

setValue(userId: string, propertyName: string, value: string | number) {
if (this.segmentEnabled) {
this.segment.identify({
userId,
traits: {
[propertyName]: value,
},
});
if (!this.segmentEnabled) {
return;
}

this.segment.identify({
userId,
traits: {
[propertyName]: value,
},
});
}

track(
name: string,
userId: string,
data: Record<string, unknown> = {},
anonymousId?: string,
) {
if (this.segmentEnabled) {
Logger.log(
`Tracking event: ${name}`,
if (!this.segmentEnabled) {
return;
}

try {
this.segment.track({
anonymousId: userId,
userId,
event: name,
properties: data,
});
} catch (error: any) {
Logger.error(
{
name,
data,
source: 'segment',
eventName: name,
usedId: userId,
message: error.message,
},
'There has been an error when tracking',
LOG_CONTEXT,
);

try {
this.segment.track({
anonymousId: userId,
userId,
event: name,
properties: data,
});
} catch (error: any) {
Logger.error(
{
eventName: name,
usedId: userId,
message: error.message,
},
'There has been an error when tracking',
LOG_CONTEXT,
);
}
}
}

Expand All @@ -155,33 +154,25 @@ export class AnalyticsService {
userId: string,
data: Record<string, unknown> = {},
) {
if (this.mixpanelEnabled) {
Logger.log(
`Tracking event: ${name}`,
if (!this.mixpanelEnabled) {
return;
}

try {
this.mixpanel.track(name, {
distinct_id: userId,
...data,
});
} catch (error: any) {
Logger.error(
{
name,
data,
source: 'mixpanel',
eventName: name,
usedId: userId,
message: error?.message,
},
'There has been an error when tracking mixpanel',
LOG_CONTEXT,
);

try {
this.mixpanel.track(name, {
distinct_id: userId,
...data,
});
} catch (error: any) {
Logger.error(
{
eventName: name,
usedId: userId,
message: error?.message,
},
'There has been an error when tracking mixpanel',
LOG_CONTEXT,
);
}
}
}

Expand Down

0 comments on commit d5b284d

Please sign in to comment.