Created
June 16, 2023 21:14
-
-
Save LuanRT/18554010275d6098cee8861fd436eacd to your computer and use it in GitHub Desktop.
YouTube.js `/analytics_data/join` endpoint example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Innertube, UniversalCache } from 'youtubei.js'; | |
(async () => { | |
const yt = await Innertube.create({ | |
cache: new UniversalCache(true) | |
}); | |
yt.session.on('auth-pending', (data) => { | |
console.log(`Go to ${data.verification_url} in your browser and enter code ${data.user_code} to authenticate.`); | |
}); | |
yt.session.on('auth', ({ credentials }) => { | |
console.log('Sign in successful:', credentials); | |
}); | |
yt.session.on('update-credentials', async ({ credentials }) => { | |
console.log('Credentials updated:', credentials); | |
await yt.session.oauth.cacheCredentials(); | |
}); | |
await yt.session.signIn(); | |
await yt.session.oauth.cacheCredentials(); | |
const response = await yt.actions.execute("/analytics_data/join", { | |
nodes: [ | |
{ | |
key: "0__DASHBOARD_FACT_ANALYTICS_CURRENT", | |
value: { | |
query: { | |
dimensions: [], | |
metrics: [ | |
{ | |
type: "VIEWS" | |
}, | |
{ | |
type: "WATCH_TIME" | |
}, | |
{ | |
type: "TOTAL_ESTIMATED_EARNINGS" | |
}, | |
{ | |
type: "SUBSCRIBERS_NET_CHANGE" | |
} | |
], | |
restricts: [ | |
{ | |
dimension: { | |
type: "USER" | |
}, | |
inValues: [ | |
"UCtjNLjY4xlhYvuwpCXakNWQ" // Your channel ID | |
] | |
} | |
], | |
orders: [], | |
timeRange: { | |
dateIdRange: { | |
inclusiveStart: 20230519, | |
exclusiveEnd: 20230616 | |
} | |
}, | |
currency: "USD", | |
returnDataInNewFormat: true, | |
limitedToBatchedData: false, | |
useMultiFormatArtistAnalytics: false | |
} | |
} | |
}, | |
{ | |
key: "0__TOP_VIDEOS", | |
value: { | |
query: { | |
dimensions: [ | |
{ | |
type: "VIDEO" | |
} | |
], | |
metrics: [ | |
{ | |
type: "VIEWS" | |
} | |
], | |
restricts: [ | |
{ | |
dimension: { | |
type: "USER" | |
}, | |
inValues: [ | |
"UCtjNLjY4xlhYvuwpCXakNWQ" // Your channel ID | |
] | |
} | |
], | |
orders: [ | |
{ | |
metric: { | |
type: "VIEWS" | |
}, | |
direction: "ANALYTICS_ORDER_DIRECTION_DESC" | |
} | |
], | |
timeRange: { | |
unixTimeRange: { | |
inclusiveStart: "1686776400", | |
exclusiveEnd: "1686949200" | |
} | |
}, | |
limit: { | |
pageSize: 3, | |
pageOffset: 0 | |
}, | |
returnDataInNewFormat: true, | |
limitedToBatchedData: false, | |
useMultiFormatArtistAnalytics: false | |
} | |
} | |
}, | |
{ | |
key: "0__DASHBOARD_FACT_ANALYTICS_LIFETIME_SUBSCRIBERS", | |
value: { | |
query: { | |
dimensions: [], | |
metrics: [ | |
{ | |
type: "SUBSCRIBERS_NET_CHANGE" | |
} | |
], | |
restricts: [ | |
{ | |
dimension: { | |
type: "USER" | |
}, | |
inValues: [ | |
"UCtjNLjY4xlhYvuwpCXakNWQ" // Your channel ID | |
] | |
} | |
], | |
orders: [], | |
timeRange: { | |
unboundedRange: {} | |
}, | |
currency: "USD", | |
returnDataInNewFormat: true, | |
limitedToBatchedData: false, | |
useMultiFormatArtistAnalytics: false | |
} | |
} | |
}, | |
{ | |
key: "0__DASHBOARD_FACT_ANALYTICS_TYPICAL", | |
value: { | |
getTypicalPerformance: { | |
query: { | |
metrics: [ | |
{ | |
metric: { | |
type: "VIEWS" | |
} | |
}, | |
{ | |
metric: { | |
type: "WATCH_TIME" | |
} | |
}, | |
{ | |
metric: { | |
type: "TOTAL_ESTIMATED_EARNINGS" | |
} | |
} | |
], | |
externalChannelId: "UCtjNLjY4xlhYvuwpCXakNWQ", // Your channel ID | |
timeRange: { | |
dateIdRange: { | |
inclusiveStart: 20230519, | |
exclusiveEnd: 20230616 | |
} | |
}, | |
type: "TYPICAL_PERFORMANCE_TYPE_NORMAL", | |
entityType: "TYPICAL_PERFORMANCE_ENTITY_TYPE_CHANNEL", | |
currency: "USD" | |
} | |
} | |
} | |
}, | |
{ | |
key: "0__TOP_VIDEOS_VIDEO", | |
value: { | |
getCreatorVideos: { | |
mask: { | |
videoId: true, | |
title: true, | |
permissions: { | |
all: true | |
} | |
} | |
} | |
} | |
} | |
], | |
connectors: [ | |
{ | |
extractorParams: { | |
resultKey: "0__TOP_VIDEOS", | |
resultTableExtractorParams: { | |
dimension: { | |
type: "VIDEO" | |
} | |
} | |
}, | |
fillerParams: { | |
targetKey: "0__TOP_VIDEOS_VIDEO", | |
idFillerParams: {} | |
} | |
} | |
], | |
allowFailureResultNodes: true, | |
trackingLabel: "web_creator_channel_dashboard_mixer" | |
}); | |
// @ts-ignore - We don't have types for this prop yet, feel free to contribute! | |
console.log(JSON.stringify(response.data.results, null, 2)) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment