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

fix(ga): Clean up old Google Analytics #324

Merged
merged 1 commit into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ DB_PORT=
NGROK_TOKEN=
PROVIDER=WITAI
ENABLE_SSL=true
GA_TRACKING_ID=
GA_V4_SECRET=
GA_V4_MEASUREMENT_ID=
NEXT_REPLICA_URL=
Expand Down
2 changes: 0 additions & 2 deletions src/__mocks__/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ export const dbPostgres = {
port: 5432,
};

export const analyticsId = `analytics-${randomIntFromInterval(1, 1000)}`;

export const analytics = {
apiSecret: `v4-secret-${randomIntFromInterval(1, 1000)}`,
measurementId: `v4-${randomIntFromInterval(1, 1000)}`,
Expand Down
2 changes: 1 addition & 1 deletion src/analytics/amplitude/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { init as initAmplitude } from "@amplitude/node";
import { customAlphabet } from "nanoid";

import { AnalyticsEventExt } from "../v4/types.js";
import { AnalyticsEventExt } from "../ga/types.js";
import { Logger } from "../../logger/index.js";
import { amplitudeToken } from "../../env.js";
import { isDevelopment } from "../../common/environment.js";
Expand Down
10 changes: 5 additions & 5 deletions src/analytics/v4/index.ts → src/analytics/ga/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AnalyticsEventExt, EVENTS_LIMIT_GA } from "./types.js";
import { analytics } from "../../env.js";
import { isDevelopment } from "../../common/environment.js";

const logger = new Logger("analytics:v4");
const logger = new Logger("analytics:ga");

export const collectEvents = (
chatId: number,
Expand All @@ -13,8 +13,8 @@ export const collectEvents = (
if (!analytics.measurementId || !analytics.apiSecret) {
if (!isDevelopment()) {
logger.error(
"v4 analytics Token is not provided!",
new Error("v4 analytics Token is not provided")
"ga analytics Token is not provided!",
new Error("ga analytics Token is not provided")
);
}
return Promise.resolve();
Expand Down Expand Up @@ -50,6 +50,6 @@ export const collectEvents = (
events,
},
})
.then(() => logger.info("Analytic data v4 has been collected"))
.catch((err) => logger.warn("Failed to collect analytic data v4", err));
.then(() => logger.info("Analytic data ga has been collected"))
.catch((err) => logger.warn("Failed to collect analytic data ga", err));
};
16 changes: 13 additions & 3 deletions src/analytics/v4/types.ts → src/analytics/ga/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface AnalyticsEventBaseParams {
page_title: string;
page_referrer: typeof TelegramApi.url;
screen_resolution: "1920x1080";
page_meta: string;
}

type AnalyticsError = {
Expand Down Expand Up @@ -62,12 +63,14 @@ const defaultLang = "not provided";

type AnalyticsAction = BotCommand | "/voice" | "/app";

export class AnalyticsDataV4 {
export class AnalyticsData {
private readonly timer: TimeMeasure;
private events: AnalyticsEvent[] = [];
private id = 0;
private lang = defaultLang;
private command: AnalyticsAction = "/app";
private commandTitle = "";
private commandMeta = "";
private title = "Audio Message Bot";

constructor(
Expand All @@ -93,12 +96,13 @@ export class AnalyticsDataV4 {
const base: AnalyticsEventBaseParams = {
app_version: this.appVersion,
page_location: `${this.url}${this.command}`,
page_title: `${this.title} ${this.command}`,
page_title: `${this.title} ${this.commandTitle || this.command}`,
thread_id: this.threadId,
engagement_time_msec: time,
language: this.lang,
screen_resolution: "1920x1080",
page_referrer: TelegramApi.url,
page_meta: this.commandMeta,
};

return this.events.map((event) => ({
Expand All @@ -115,8 +119,14 @@ export class AnalyticsDataV4 {
return this;
}

public setCommand(command: AnalyticsAction): this {
public setCommand(
command: AnalyticsAction,
commandTitle: string,
commandMeta = ""
): this {
this.command = command;
this.commandTitle = commandTitle;
this.commandMeta = commandMeta;
return this;
}

Expand Down
26 changes: 7 additions & 19 deletions src/analytics/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import { AnalyticsData } from "./legacy/types.js";
import { collectEvents as collectLegacy } from "./legacy/index.js";
import { analyticsId } from "../env.js";
import { Logger } from "../logger/index.js";
import { flattenPromise } from "../common/helpers.js";
import { BotCommand } from "../telegram/types.js";
import { collectEvents as collectV4 } from "./v4/index.js";
import { collectEvents as collectGA } from "./ga/index.js";
import { collectEvents as collectAmplitude } from "./amplitude/index.js";
import { AnalyticsData } from "./ga/types.js";

const logger = new Logger("analytics");

export const collectAnalytics = (analytics: AnalyticsData): Promise<void> => {
logger.info("Collecting analytic data");
const evts = analytics.v4.getEvents();
const evts = analytics.getEvents();
const id = analytics.getId();

return Promise.all([
analytics.getListDto(analyticsId).map((dto) => collectLegacy(dto)),
collectV4(analytics.v4.getId(), evts),
collectAmplitude(analytics.v4.getId(), evts),
]).then(flattenPromise);
};

export const collectPageAnalytics = (
analytics: AnalyticsData,
page: BotCommand | "/voice"
): Promise<void> => {
logger.info("Collecting page analytic data");
return collectLegacy(analytics.getPageDto(analyticsId, page));
return Promise.all([collectGA(id, evts), collectAmplitude(id, evts)])
.then(flattenPromise)
.catch((err) => logger.warn("Failed to collect analytic data", err));
};
39 changes: 0 additions & 39 deletions src/analytics/legacy/index.ts

This file was deleted.

Loading