Automate YouTube Analytics with Jenkins: Retrieving Video Views and Likes

Ayushmaan Srivastav
3 min readOct 25, 2024

--

Tracking YouTube analytics can offer valuable insights for creators and marketers, but manually fetching these metrics can be time-consuming. This blog covers how to automate the retrieval of views and likes for YouTube videos using a Jenkins job. By the end, you’ll have a Jenkins job that automatically fetches analytics data using Python and the YouTube API, helping you save time and stay up-to-date.

Prerequisites

  1. Jenkins Installed: Make sure Jenkins is installed and running.
  2. Google API Key: Obtain an API key from the Google Developers Console for YouTube Data API v3.
  3. Python Installed: Install Python on your Jenkins server and necessary packages.

Step 1: Obtain a YouTube API Key

  1. Go to the Google Cloud Console.
  2. Create a new project or select an existing project.
  3. Go to APIs & ServicesLibrary.
  4. Search for YouTube Data API v3 and enable it.
  5. Go to APIs & ServicesCredentialsCreate CredentialsAPI Key.
  6. Copy the API key, as you’ll use it in the Python script.

Step 2: Install Python Packages

Install the required Python libraries:

pip install requests

Step 3: Write the Python Script for Retrieving Views and Likes

Create a Python script, youtube_metrics.py, to retrieve YouTube views and likes based on the video ID.

# youtube_metrics.py
import requests
import sys

# YouTube API Key and Video ID passed as command-line arguments
api_key = sys.argv[1]
video_id = sys.argv[2]

def get_youtube_metrics(api_key, video_id):
url = f"https://www.googleapis.com/youtube/v3/videos?part=statistics&id={video_id}&key={api_key}"
response = requests.get(url)

if response.status_code == 200:
data = response.json()
if "items" in data and data["items"]:
stats = data["items"][0]["statistics"]
views = stats.get("viewCount", "N/A")
likes = stats.get("likeCount", "N/A")
print(f"Views: {views}, Likes: {likes}")
else:
print("No data found for the given video ID.")
else:
print("Failed to retrieve data:", response.status_code)

# Run the function
get_youtube_metrics(api_key, video_id)

Step 4: Configure the Jenkins Job

  1. Create a Freestyle Project:
  • Go to New ItemFreestyle project and name it YouTube Metrics Retrieval.

2. Configure the Project:

  • In General Settings, you can add a description.

3. Build Environment:

  • If using credentials for the API key, configure it under Use secret text(s) or file(s).

4. Add Build Steps:

  • Select Execute Shell as the build step.
  • Add the following shell command, replacing /path/to/youtube_metrics.py with your Python script location and providing the video_id of the YouTube video.
# Execute the Python script with API key and video ID as arguments
python3 /path/to/youtube_metrics.py <your_api_key> <your_video_id>

5. Post-Build Actions (Optional):

  • To save the output, you could add a post-build action to archive the console output or configure email notifications.

6. Save the Job:

  • Click Save to complete the job configuration.

Step 5: Run and Monitor the Job

  1. Click Build Now to start the job.
  2. Go to Console Output to view the results. You should see the views and likes retrieved from the YouTube API displayed in the console.

Automating with Scheduled Builds (Optional)

  1. In the Build Triggers section, select Build periodically and set a schedule in cron format, e.g., H H * * * for a daily job.
  2. Jenkins will now retrieve YouTube views and likes automatically based on the schedule.

Conclusion

This Jenkins job offers an efficient way to keep track of your YouTube video analytics automatically. With the power of Jenkins and YouTube’s Data API, you can integrate analytics retrieval into your automated workflows, making it easy to monitor engagement and adapt your strategies accordingly.

--

--

No responses yet