@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
132 lines (131 loc) • 5.34 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAnalyticsProperty = exports.listAnalyticsProperties = exports.getAnalyticsAccount = void 0;
// Imports the Google Analytics Admin API client library
const admin_1 = require("@google-analytics/admin");
const chalk_1 = __importDefault(require("chalk"));
const cli_table_1 = __importDefault(require("cli-table"));
const log_1 = require("diginext-utils/dist/xconsole/log");
const const_1 = require("../../config/const");
const app_helper_1 = require("../apps/app-helper");
// const { Account } = v1alpha;
const { google } = admin_1.protos;
// google.analytics.admin.v1alpha.Account
// Instantiates a client using default credentials.
let analyticsAdminClient = new admin_1.AnalyticsAdminServiceClient({ keyFilename: const_1.ANALYTICS_SA_PATH });
// TODO: update fetch command from Analytics API
async function getAnalyticsAccount() {
// Calls listAccounts() method of the Google Analytics Admin API and prints
// the response for each account.
// const [accounts] = await analyticsAdminClient.listAccounts();
// accounts.forEach((account) => log(account));
// return accounts[0];
return null;
}
exports.getAnalyticsAccount = getAnalyticsAccount;
const listAnalyticsProperties = async () => {
const account = await getAnalyticsAccount();
// const properties = await analyticsAdminClient.listProperties({filter: })
return account;
};
exports.listAnalyticsProperties = listAnalyticsProperties;
const createAnalyticsProperty = async (app, { env = "dev", name, url }) => {
const appConfig = (0, app_helper_1.getAppConfigFromApp)(app);
// if (!name) logError(`"name" is required`);
// if (!url) logError(`"url" is required`);
const accountName = name || appConfig.name;
let websiteUrl = url || (appConfig.deployEnvironment[env].domains && appConfig[env].domains[0]);
if (typeof websiteUrl == "undefined" && env == "dev")
websiteUrl = `https://dev3.digitop.vn/${appConfig.slug}`;
if (!websiteUrl)
(0, log_1.logError)(`[${env.toUpperCase()}] Website URL is required, double check domain name in your app config on Diginext workspace.`);
const account = await getAnalyticsAccount();
// await analyticsAdminClient.updateAccount({ account: account });
// await analyticsAdminClient.provisionAccountTicket({account: account})
let properties, property;
let webStreams, webStream;
// // delete properties
// try {
// properties = await analyticsAdminClient.listProperties({ filter: `parent:${account.name}` });
// log(`properties:`, properties[0]);
// } catch (e) {
// logError(e);
// }
// await Promise.all(
// properties[0]
// .filter((prop) => prop.displayName == "Digichat")
// .map((prop) => analyticsAdminClient.deleteProperty({ name: prop.name }))
// );
// // list properties
// try {
// properties = await analyticsAdminClient.listProperties({ filter: `parent:${account.name}` });
// log(`properties:`, properties[0]);
// } catch (e) {
// logError(e);
// }
// return;
// // check web stream existed
// try {
// webStreams = await analyticsAdminClient.listWebDataStreams({ filter: `parent:${property.name}` });
// } catch (e) {
// logError(e);
// }
try {
const createPropertyResults = await analyticsAdminClient.createProperty({
property: {
parent: account.name,
displayName: accountName,
currencyCode: "VND",
// countryCode: "vi",
industryCategory: "ARTS_AND_ENTERTAINMENT",
timeZone: "Asia/Saigon",
},
});
property = createPropertyResults[0];
// log(req);
}
catch (e) {
(0, log_1.logError)(e);
}
// SDK.v1alpha.AnalyticsAdminServiceClient
try {
webStreams = await analyticsAdminClient.createWebDataStream({
parent: property.name,
webDataStream: {
defaultUri: websiteUrl,
displayName: accountName,
},
});
webStream = webStreams[0];
let table = new cli_table_1.default();
table.push(["GA4 ID:", chalk_1.default.cyan(webStream.measurementId)]);
(0, log_1.log)(`\n` + table.toString());
}
catch (e) {
(0, log_1.logError)(e);
}
// log(webStream);
// log(webStream.name);
// let tag;
// try {
// const tags = await analyticsAdminClient.getGlobalSiteTag({ name: webStream.name + "/globalSiteTag" });
// tag = tags[0];
// log("Global Site Tag:", tag);
// } catch (e) {
// logError(e);
// }
// diginext.ga = diginext.ga || {};
// diginext.ga[env] = diginext.ga[env] ? [...diginext.ga[env], webStream.measurementId] : [webStream.measurementId];
};
exports.createAnalyticsProperty = createAnalyticsProperty;
// main(...process.argv.slice(2)).catch((err) => {
// console.error(err.message);
// process.exitCode = 1;
// });
// process.on("unhandledRejection", (err) => {
// console.error(err.message);
// process.exitCode = 1;
// });