Skip to content
Open
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
"dependencies": {
"@google-analytics/admin": "^8.2.0",
"@google-analytics/data": "^5.1.0",
"@sentry/node": "^9.0.0",
"dotenv": "^16.5.0",
"effect": "^3.15.4",
"googleapis": "^150.0.1",
"inversify": "^7.5.1",
"node-fetch": "^3.3.2",
"reflect-metadata": "^0.2.2",
"@sentry/node": "^9.0.0",
"yaml": "^2.5.1"
}
}
100 changes: 100 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/application/query/SearchConsoleQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Effect } from "effect";
import type { SearchConsoleWebsiteData } from "../../domain/SearchConsole.js";
import type { WebsiteConfig } from "../../domain/WebsiteConfig.js";

export interface SearchConsoleQuery {
getSearchConsoleDataByWebsites(
websites: WebsiteConfig[],
): Effect.Effect<SearchConsoleWebsiteData[], Error>;
}
7 changes: 7 additions & 0 deletions src/config/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Container } from "inversify";
import "reflect-metadata";
import type { PVQuery } from "../application/query/PVQuery.js";
import { Ga4PVQueryAdapter } from "../infrastructure/query/GA4PVQueryAdapter.js";
import type { SearchConsoleQuery } from "../application/query/SearchConsoleQuery.js";
import { SearchConsolePVQueryAdapter } from "../infrastructure/query/SearchConsolePVQueryAdapter.js";
import type { SlackCommand } from "../application/command/SlackCommand.js";
import { SlackCommandAdapter } from "../infrastructure/command/SlackCommandAdapter.js";
import { envConfig } from "./EnvConfig.js";
Expand All @@ -26,6 +28,11 @@ container

container.bind<PVQuery>(TYPES.PVQuery).to(Ga4PVQueryAdapter).inSingletonScope();

container
.bind<SearchConsoleQuery>(TYPES.SearchConsoleQuery)
.to(SearchConsolePVQueryAdapter)
.inSingletonScope();

container
.bind<SlackCommand>(TYPES.SlackCommand)
.to(SlackCommandAdapter)
Expand Down
1 change: 1 addition & 0 deletions src/config/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const TYPES = {
SentryDsn: Symbol.for("SentryDsn"),
},
PVQuery: Symbol.for("PQuery"),
SearchConsoleQuery: Symbol.for("SearchConsoleQuery"),
SlackCommand: Symbol.for("SlackCommand"),
WebMetricsCollector: Symbol.for("WebMetricsCollector"),
ErrorReporter: Symbol.for("ErrorReporter"),
Expand Down
8 changes: 8 additions & 0 deletions src/domain/SearchConsole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface SearchConsoleWebsiteData {
websiteName: string;
siteUrl: string;
clicks: number;
impressions: number;
ctr: number;
position: number;
}
59 changes: 59 additions & 0 deletions src/infrastructure/query/SearchConsolePVQueryAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Effect } from "effect";
import type { SearchConsoleWebsiteData } from "../../domain/SearchConsole.js";
import type { WebsiteConfig } from "../../domain/WebsiteConfig.js";
import type { SearchConsoleQuery } from "../../application/query/SearchConsoleQuery.js";
import { google } from "googleapis";
import { inject, injectable } from "inversify";
import { TYPES } from "../../config/Types.js";

@injectable()
export class SearchConsolePVQueryAdapter implements SearchConsoleQuery {
private readonly auth: any;
private readonly webmasters: any;

constructor(@inject(TYPES.config.GoogleKeyFilePath) path: string) {
this.auth = new google.auth.GoogleAuth({
keyFile: path,
scopes: ["https://www.googleapis.com/auth/webmasters.readonly"],
});
this.webmasters = google.webmasters({
version: "v3",
auth: this.auth,
});
}

getSearchConsoleDataByWebsites(
websites: WebsiteConfig[],
): Effect.Effect<SearchConsoleWebsiteData[], Error> {
const websitesWithSearchConsole = websites.filter(
(website) => website.metrics.searchConsole?.siteUrl,
);

return Effect.all(
websitesWithSearchConsole.map((website) =>
Effect.promise(async () => {
const siteUrl = website.metrics.searchConsole!.siteUrl;
const result = await this.webmasters.searchanalytics.query({
siteUrl: siteUrl,
requestBody: {
startDate: "yesterday",
endDate: "yesterday",
dimensions: [],
rowLimit: 1,
},
});

const row = result.data.rows?.[0];
return {
websiteName: website.name,
siteUrl: siteUrl,
clicks: row?.clicks || 0,
impressions: row?.impressions || 0,
ctr: row?.ctr || 0,
position: row?.position || 0,
};
}),
),
);
}
}