Skip to content

Commit

Permalink
Added new action create key event and components refactored (#12616)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcortes committed Jun 28, 2024
1 parent 6dd2880 commit b8fdfd2
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import googleAnalytics from "../../google_analytics.app.mjs";
import {
INDUSTRY_CATEGORY_OPTIONS, TIMEZONE_OPTIONS,
} from "../../common/constants.mjs";
import constants from "../../common/constants.mjs";

export default {
key: "google_analytics-create-ga4-property",
name: "Create GA4 Property",
description: "Creates a new GA4 property. [See the documentation](https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1beta/properties/create)",
version: "0.0.1",
version: "0.1.0",
type: "action",
props: {
googleAnalytics,
Expand All @@ -26,14 +24,14 @@ export default {
type: "string",
label: "Time Zone",
description: "The reporting time zone for the property. Must be a valid value from [the IANA timezone database](https://www.iana.org/time-zones).",
options: TIMEZONE_OPTIONS,
options: constants.TIMEZONE_OPTIONS,
},
industryCategory: {
type: "string",
label: "Industry Category",
description: "The industry category associated with the property.",
optional: true,
options: INDUSTRY_CATEGORY_OPTIONS,
options: constants.INDUSTRY_CATEGORY_OPTIONS,
},
currencyCode: {
type: "string",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import app from "../../google_analytics.app.mjs";

export default {
key: "google_analytics-create-key-event",
name: "Create Key Event",
description: "Creates a new key event. [See the documentation](https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1beta/properties.keyEvents/create)",
version: "0.0.1",
type: "action",
props: {
app,
parent: {
type: "string",
label: "Parent",
description: "The resource name of the parent property where this Key Event will be created. Format: `properties/123`",
},
eventName: {
type: "string",
label: "Event Name",
description: "Immutable. The event name for this key event. Examples: `click`, `purchase`",
},
countingMethod: {
type: "string",
label: "Counting Method",
description: "The method by which Key Events will be counted across multiple events within a session.",
options: [
{
label: "Counting method not specified.",
value: "COUNTING_METHOD_UNSPECIFIED",
},
{
label: "Each Event instance is considered a Key Event.",
value: "ONCE_PER_EVENT",
},
{
label: "An Event instance is considered a Key Event at most once per session per user.",
value: "ONCE_PER_SESSION",
},
],
},
},
methods: {
createKeyEvent({
parent, ...args
} = {}) {
return this.app.post({
path: `/${parent}/keyEvents`,
...args,
});
},
},
async run({ $ }) {
const {
createKeyEvent,
parent,
eventName,
countingMethod,
} = this;

const response = await createKeyEvent({
$,
parent,
data: {
eventName,
countingMethod,
},
});

$.export("$summary", `Successfully created key event with name ${eventName} and counting method ${countingMethod} in parent ${parent}`);
return response;
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import analytics from "../../google_analytics.app.mjs";

export default {
key: "google_analytics-run-report-in-ga4",
version: "0.0.3",
version: "0.1.0",
name: "Run Report in GA4",
description: "Returns a customized report of your Google Analytics event data. Reports contain statistics derived from data collected by the Google Analytics tracking code. [See the documentation here](https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport)",
type: "action",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import analytics from "../../google_analytics.app.mjs";

export default {
key: "google_analytics-run-report",
version: "0.0.3",
version: "0.1.0",
name: "Run Report",
description: "Return report metrics based on a start and end date. [See the docs here](https://developers.google.com/analytics/devguides/reporting/core/v4/rest?hl=en)",
type: "action",
Expand Down
21 changes: 19 additions & 2 deletions components/google_analytics/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const INDUSTRY_CATEGORY_OPTIONS = [
const INDUSTRY_CATEGORY_OPTIONS = [
{
value: "INDUSTRY_CATEGORY_UNSPECIFIED",
label: "Industry category unspecified",
Expand Down Expand Up @@ -109,7 +109,7 @@ export const INDUSTRY_CATEGORY_OPTIONS = [
},
];

export const TIMEZONE_OPTIONS = [
const TIMEZONE_OPTIONS = [
"Africa/Abidjan",
"Africa/Algiers",
"Africa/Bissau",
Expand Down Expand Up @@ -380,3 +380,20 @@ export const TIMEZONE_OPTIONS = [
"Pacific/Tarawa",
"Pacific/Tongatapu",
];

const API = {
DATA: {
BASE_URL: "https://analyticsdata.googleapis.com",
VERSION_PATH: "/v1beta",
},
ADMIN: {
BASE_URL: "https://analyticsadmin.googleapis.com",
VERSION_PATH: "/v1beta",
},
};

export default {
INDUSTRY_CATEGORY_OPTIONS,
TIMEZONE_OPTIONS,
API,
};
9 changes: 9 additions & 0 deletions components/google_analytics/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function monthAgo() {
const monthAgo = new Date();
monthAgo.setMonth(monthAgo.getMonth() - 1);
return monthAgo.toISOString().split("T")[0];
}

export default {
monthAgo,
};
70 changes: 37 additions & 33 deletions components/google_analytics/google_analytics.app.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import analyticsreporting from "@googleapis/analyticsreporting";
import { axios } from "@pipedream/platform";
import constants from "./common/constants.mjs";

export default {
type: "app",
Expand All @@ -24,65 +25,68 @@ export default {
},
},
methods: {
monthAgo() {
const monthAgo = new Date();
monthAgo.setMonth(monthAgo.getMonth() - 1);
return monthAgo.toISOString().split("T")[0];
},
_accessToken() {
return this.$auth.oauth_access_token;
},
client() {
const auth = new analyticsreporting.auth.OAuth2();
auth.setCredentials({
access_token: this._accessToken(),
});
return analyticsreporting.analyticsreporting({
version: "v4",
auth,
});
},
queryReports(data) {
const client = this.client();
return client.reports.batchGet(data);
},
getHeaders(headers = {}) {
return {
authorization: `Bearer ${this._accessToken()}`,
...headers,
};
},
makeRequest(customConfig) {
const {
$ = this,
headers,
...otherConfig
} = customConfig;

getUrl(path, api = constants.API.ADMIN) {
return `${api.BASE_URL}${api.VERSION_PATH}${path}`;
},
makeRequest({
$ = this, path, headers, api, ...args
} = {}) {
const config = {
url: this.getUrl(path, api),
headers: this.getHeaders(headers),
...otherConfig,
...args,
};
return axios($, config);
},
async queryReportsGA4(args = {}) {
post(args = {}) {
return this.makeRequest({
url: `https://analyticsdata.googleapis.com/v1beta/properties/${args.property}:runReport`,
method: "POST",
...args,
});
},
async listAccounts() {
return this.makeRequest({
url: "https://analyticsadmin.googleapis.com/v1beta/accounts",
createProperty(args) {
return this.post({
path: "/properties",
...args,
});
},
async createProperty(args) {
listAccounts(args = {}) {
return this.makeRequest({
url: "https://analyticsadmin.googleapis.com/v1beta/properties",
method: "POST",
path: "/accounts",
...args,
});
},
client() {
const auth = new analyticsreporting.auth.OAuth2();
auth.setCredentials({
access_token: this._accessToken(),
});
return analyticsreporting.analyticsreporting({
version: "v4",
auth,
queryReportsGA4({
property, ...args
} = {}) {
return this.post({
api: constants.API.DATA,
path: `/properties/${property}:runReport`,
...args,
});
},
async queryReports(data) {
const client = this.client();
return client.reports.batchGet(data);
},
},
};
4 changes: 2 additions & 2 deletions components/google_analytics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/google_analytics",
"version": "0.1.0",
"version": "0.2.0",
"description": "Pipedream Google_analytics Components",
"main": "google_analytics.app.mjs",
"keywords": [
Expand All @@ -14,6 +14,6 @@
},
"dependencies": {
"@googleapis/analyticsreporting": "^1.0.0",
"@pipedream/platform": "^1.2.0"
"@pipedream/platform": "^3.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import analytics from "../../google_analytics.app.mjs";
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
import utils from "../../common/utils.mjs";

export default {
key: "google_analytics-page-opened",
version: "0.0.4",
version: "0.1.0",
name: "New Page Opened",
description: "Emit new event when a page is viewed",
type: "source",
Expand All @@ -26,7 +27,7 @@ export default {
},
hooks: {
async deploy() {
const startDate = this.analytics.monthAgo();
const startDate = utils.monthAgo();
this._setStartDate(startDate);
await this.processEvent();
},
Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

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

0 comments on commit b8fdfd2

Please sign in to comment.