Skip to content
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
119 changes: 119 additions & 0 deletions components/youtube_analytics_api/actions/common/reports-query.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import app from "../../youtube_analytics_api.app.mjs";
import constants from "../../common/constants.mjs";
import utils from "../../common/utils.mjs";
import propsFragments from "../../common/props-fragments.mjs";

export default {
props: {
app,
reloader: {
type: "boolean",
label: "Hidden Reloader",
description: "This prop is used to reload the props when the step gets created.",
hidden: true,
reloadProps: true,
},
Comment on lines +9 to +15
Copy link
Collaborator

@michelle0927 michelle0927 Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😮 Cool use of the reloader prop! I didn't know that would work.

startDate: {
propDefinition: [
app,
"startDate",
],
},
endDate: {
propDefinition: [
app,
"endDate",
],
},
dimensions: {
propDefinition: [
app,
"dimensions",
],
},
sort: {
propDefinition: [
app,
"sort",
],
},
maxResults: {
propDefinition: [
app,
"maxResults",
],
},
},
methods: {
getIdsProps() {
const { idType } = this;

if (idType === constants.ID_TYPE.CHANNEL.value) {
return {
idType: propsFragments.idType,
};
}

if (idType === constants.ID_TYPE.CONTENT_OWNER.value) {
return {
idType: propsFragments.idType,
ids: {
type: "string",
label: "Content Owner Name",
description: "The content owner name for the user. Eg. `MyContentOwnerName`.",
},
};
}

if (idType === constants.ID_TYPE.CHANNEL_ID.value) {
return {
idType: propsFragments.idType,
ids: {
type: "string",
label: "Channel ID",
description: "The channel ID for the user. Eg. `UC_x5XG1OV2P6uZZ5FSM9Ttw`. You can find the ID using the [YouTube Data API](https://developers.google.com/youtube/v3/docs/channels/list).",
},
};
}

return {
idType: propsFragments.idType,
};
},
getIdsParam() {
const {
idType,
ids,
} = this;
if (idType === constants.ID_TYPE.CHANNEL.value) {
return "channel==MINE";
}
if (idType === constants.ID_TYPE.CONTENT_OWNER.value) {
return `contentOwner==${ids}`;
}
if (idType === constants.ID_TYPE.CHANNEL_ID.value) {
return `channel==${ids}`;
}
},
getFiltersParam() {
const { filters } = this;
const filtersObj = utils.parseJson(filters);

if (!filtersObj) {
return;
}

return utils.arrayToCommaSeparatedList(
Object.entries(filtersObj)
.reduce((acc, [
key,
val,
]) => [
...acc,
`${key}==${val}`,
], []),
";",
);
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import common from "../common/reports-query.mjs";
import utils from "../../common/utils.mjs";
import propsFragments from "../../common/props-fragments.mjs";

export default {
...common,
key: "youtube_analytics_api-get-video-metrics",
name: "Get Video Metrics",
description: "Retrieve detailed analytics for a specific video. [See the documentation](https://developers.google.com/youtube/analytics/reference/reports/query)",
version: "0.0.1",
type: "action",
props: {
...common.props,
videoId: {
type: "string",
label: "Video ID",
description: "The ID of the video for which you want to retrieve metrics. Eg. `pd1FJh59zxQ`.",
},
metrics: propsFragments.metrics,
},
additionalProps() {
return this.getIdsProps();
},
async run({ $ }) {
const {
app,
videoId,
getIdsParam,
startDate,
endDate,
metrics,
dimensions,
sort,
maxResults,
} = this;

const response = await app.reportsQuery({
$,
params: {
ids: getIdsParam(),
startDate,
endDate,
metrics: utils.arrayToCommaSeparatedList(metrics),
dimensions: utils.arrayToCommaSeparatedList(dimensions),
sort: utils.arrayToCommaSeparatedList(sort),
maxResults,
filters: `video==${videoId}`,
},
});

$.export("$summary", "Successfully fetched video metrics.");
return response;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import common from "../common/reports-query.mjs";
import constants from "../../common/constants.mjs";
import utils from "../../common/utils.mjs";
import propsFragments from "../../common/props-fragments.mjs";

export default {
...common,
key: "youtube_analytics_api-list-channel-reports",
name: "List Channel Reports",
description: "Fetch summary analytics reports for a specified youtube channel. Optional filters include date range and report type. [See the documentation](https://developers.google.com/youtube/analytics/reference/reports/query)",
version: "0.0.1",
type: "action",
additionalProps() {
const {
getIdsProps,
getReportTypeProps,
} = this;

return {
...getIdsProps(),
...getReportTypeProps(),
};
},
methods: {
...common.methods,
getReportTypeProps() {
const { channelReportType } = this;
const {
VIDEO_BASIC_USER_ACTIVITY_STATS,
PLAYLIST_BASIC_STATS,
} = constants.CHANNEL_REPORT_TYPE;

if (channelReportType === VIDEO_BASIC_USER_ACTIVITY_STATS.value) {
const supportedFilters = VIDEO_BASIC_USER_ACTIVITY_STATS.metadata.filters
.reduce((acc, filter) => ({
...acc,
[filter]: "",
}), {});

return {
channelReportType: propsFragments.channelReportType,
metrics: {
...propsFragments.metrics,
options: VIDEO_BASIC_USER_ACTIVITY_STATS.metadata.metrics,
},
filters: {
...propsFragments.filters,
description: `**Supported filters: \`${JSON.stringify(supportedFilters)}\`**. ${propsFragments.filters.description}`,
},
};
}

if (channelReportType === PLAYLIST_BASIC_STATS.value) {
const supportedFilters = PLAYLIST_BASIC_STATS.metadata.filters
.reduce((acc, filter) => ({
...acc,
[filter]: "",
}), {});

return {
channelReportType: propsFragments.channelReportType,
metrics: {
...propsFragments.metrics,
options: PLAYLIST_BASIC_STATS.metadata.metrics,
},
filters: {
...propsFragments.filters,
description: `**Supported filters: \`${JSON.stringify(supportedFilters)}\`**. ${propsFragments.filters.description}`,
},
};
}

return {
channelReportType: propsFragments.channelReportType,
};
},
},
async run({ $ }) {
const {
app,
getIdsParam,
getFiltersParam,
startDate,
endDate,
metrics,
sort,
maxResults,
} = this;

const response = await app.reportsQuery({
$,
params: {
ids: getIdsParam(),
startDate,
endDate,
metrics: utils.arrayToCommaSeparatedList(metrics),
filters: getFiltersParam(),
sort: utils.arrayToCommaSeparatedList(sort),
maxResults,
},
});

$.export("$summary", "Successfully fetched channel reports.");
return response;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import common from "../common/reports-query.mjs";
import utils from "../../common/utils.mjs";
import propsFragments from "../../common/props-fragments.mjs";

export default {
...common,
key: "youtube_analytics_api-query-custom-analytics",
name: "Query Custom Analytics",
description: "Execute a custom analytics query using specified metrics, dimensions, filters, and date ranges. Requires query parameters to configure. [See the documentation](https://developers.google.com/youtube/analytics/reference/reports/query).",
version: "0.0.1",
type: "action",
props: {
...common.props,
metrics: propsFragments.metrics,
filters: propsFragments.filters,
},
additionalProps() {
return this.getIdsProps();
},
async run({ $ }) {
const {
app,
getIdsParam,
getFiltersParam,
startDate,
endDate,
metrics,
dimensions,
sort,
maxResults,
} = this;

const response = await app.reportsQuery({
$,
params: {
ids: getIdsParam(),
startDate,
endDate,
metrics: utils.arrayToCommaSeparatedList(metrics),
dimensions: utils.arrayToCommaSeparatedList(dimensions),
filters: getFiltersParam(),
sort: utils.arrayToCommaSeparatedList(sort),
maxResults,
},
});

$.export("$summary", "Successfully fetched custom analytics data.");
return response;
},
};
Loading
Loading