Making use of YouTube data is much more than simply statistics; it's about knowing your audience's heartbeat. You can discover stuff that resonates by following metrics like views, likes, shares, and comments. These metrics act as breadcrumbs. Having such information at your disposal makes developing a successful channel strategy not only realistic but also easy.
Why YouTube Analytics API?
YouTube Analytics API vs. YouTube Data API
How to Discover Insights of Youtube Channels and Content with Youtube Analytics API with React NextJs
Set up your Project on Google Cloud Console
Create a Project on Google Cloud Console. Go to the Google Cloud Console. Create a new project or select an existing project.
Now switch to API Library.
Enable YouTube Analytics API
Set up OAuth Credentials
Now, click on the "Configure consent screen".
Add users by providing a valid email address.
And "save and continue" to proceed further.
Install Necessary Dependencies for your NextJs Project
npm install googleapis next-auth
Set up NextAuth
Create an auth folder in the Pages folder and paste this code.
import NextAuth from 'next-auth';
import Providers from 'next-auth/providers';
export default NextAuth({
providers: [
Providers.Google({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
authorizationUrl: 'https://accounts.google.com/o/oauth2/auth?response_type=code&prompt=consent&access_type=offline&scope=https://www.googleapis.com/auth/youtube.readonly'
})
],
callbacks: {
async jwt(token, account) {
if (account) {
token.accessToken = account.accessToken;
token.refreshToken = account.refreshToken;
}
return token;
},
async session(session, token) {
session.accessToken = token.accessToken;
return session;
}
}
});
Fetch Youtube Data API
Create API routes in the pages/api directory to interact with the YouTube API Statistics:
import { google } from 'googleapis';
import { getSession } from 'next-auth/client';
export default async (req, res) => {
const session = await getSession({ req });
if (!session) {
return res.status(401).json({ error: 'Unauthorized' });
}
const youtube = google.youtube({
version: 'v3',
auth: session.accessToken
});
try {
const response = await youtube.channels.list({
part: 'statistics',
mine: true
});
res.status(200).json(response.data);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
And also to display videos.
import { google } from 'googleapis';
import { getSession } from 'next-auth/client';
export default async (req, res) => {
const session = await getSession({ req });
if (!session) {
return res.status(401).json({ error: 'Unauthorized' });
}
const youtube = google.youtube({
version: 'v3',
auth: session.accessToken
});
try {
const response = await youtube.search.list({
part: 'snippet',
forMine: true,
type: 'video',
maxResults: 10
});
res.status(200).json(response.data);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
Create index.js
Set up .env
GOOGLE_CLIENT_ID=your-client-id
GOOGLE_CLIENT_SECRET=your-client-secret
NEXTAUTH_URL=http://localhost:3000
Run your Application
npm run dev
And we are done✌
Frequently Asked Questions
1. What is the API limit for YouTube Analytics?
The youtube api for developers use is limited. Each Google Cloud project you start has a default restriction of 10,000 "units" each day. The term "cost" refers to API usage limitations rather than monetary costs. Using the API is free.
2. How to get YouTube data API?
To get access to the YouTube Data API:
- Create a Google Cloud Platform project.
- Enable the YouTube Data API v3 in the project.
- Generate OAuth 2.0 credentials or an API key.
- Set up authorization if using OAuth.
- Start making requests to the API endpoints using the credentials or API key.
3. What is the difference between YouTube Data API and YouTube Analytics API?
YouTube Data API provides access to YouTube's content, such as videos, playlists, and channels. YouTube Analytics API focuses on channel and video analytics, offering insights into performance metrics like views and engagement.
4. How to increase YouTube API limit?
To increase YouTube API limits:
- 1. Apply for higher quota limits via the Google Cloud Console.
- 2. Provide justification for increased usage.
- 3. Await approval from Google.
5. What is youtube iframe api?
6. What is youtube data api?
7. What is my youtube api key?
- Go to the Google Developers Console
- Create a new project
- Enable the YouTube Data API
- Create credentials
- Get API Key
8. How to increase youtube api quota?
- Sign in to Google Cloud Console
- Select your project
- Navigate to the Quotas page
- Select YouTube Data API
- Request quota increase
- Submit your request