The Google Search Console API Data Fetcher is a Java-based tool designed to automate the process of fetching data from the Google Search Console API and storing it into a MySQL database. This project uses Maven for easy build and deployment and can be executed as a serverless application on AWS Lambda. With this tool, you can streamline your search analytics data collection, ensuring your SEO strategies are data-driven and up-to-date.
- Automated Data Fetching: Seamlessly fetch search analytics data from Google Search Console.
- Serverless Deployment: Deploy on AWS Lambda for cost-effective and scalable operation.
- Customizable: Fully documented code for easy customization and adaptation.
- Comprehensive Setup: Includes all necessary files and a step-by-step guide for setting up and running the project.
- Daily Execution: Schedule automatic daily data fetch to keep your analytics up-to-date.
- Overview
- Setup Guide
- Code Explanation
- Crucial Variables to Replace
- Running Tests Locally
- Customizing the Code
- Troubleshooting
To view the image in its original quality, click on the images.
-
Go to the Google Developers Console. And click on “CREATE PROJECT“ in the top menu. (If you are not logged in to your Google account, you will need to log in first before you can generate the Google Search Console API credentials. This is because the Google Search Console API requires authentication with a valid Google account in order to access the data and perform actions.)
-
Specify the name of the desired project and the address of your project file. click on the “CREATE” button to create your project.
-
After registering your project, a successful registration notification will be displayed for you. Select your project to enter its environment
-
To set up the API, click on “Go to APIs overview”
-
Click on the “Library” or “Enable APIs and services”.
-
Search for the phrase "Google Search Console API" in the search box API.
-
On the search result click on the google search console.
-
Click on the “Enable“ button.
-
Go to the Credentials page. Click on the “Create credentials“ option in the top menu and select “OAuth client ID”.
-
Select the “Web application” option
-
After selecting Web application, some fields will be displayed for you. Enter a desired name and the name of the website you would like to get information about from the Google Search Console, enter it below. then click on the “CREATE“ button
-
Click on the “DOWNLOAD JSON”. -Please note that this file is only available for download once, so it's best to upload it to a secure space and provide you with a direct link. In the next steps, you will need the direct address of this file.
In general, to compile this project, you just need to clone the repository and replace the 4 variables specified in the source code with your desired information, then follow the compilation steps.
-
To clone the repository:
git clone https://github.com/erekhshe/gscFetcher.git
-
The compiled Jar file will be located in the
target
directory.
The 4 specific variables that need to be replaced with your desired information are:
-
Website URL: Replace
"https://www.example.com"
with your own website URL in theMyGSCFetcherFunction
method.String websiteUrl = "https://www.example.com";
-
Database Connection String: Replace
"1.1.1.1:3306/database_name?user=user&password=password"
with your own MySQL database connection string in thesaveToDatabase
method.String connectionString = "1.1.1.1:3306/database_name?user=user&password=password";
-
Table Name: The name of the table in the database where you want to store the information
String tableName = "your_table_name";
-
Credential URL: Replace
"https://example.s3.amazonaws.com/gsc-credential.json"
with the URL to your uploaded JSON credential file in thebuildWebmasters
method.String credentialUrl = "https://example.s3.amazonaws.com/gsc-credential.json";
In general, the compilation can be done with the command "mvn clean install"
mvn clean install
here we will explain the complete compilation process for Windows using the Eclipse IDE.
Follow the steps below
-
Open Eclipse and Click on File > New > Maven Project.
-
Check the "Create a simple project" box and click Next.
-
Fill the first two fields with desired names, then click Finish.
-
Download the project from GitHub and replace the internal files in the eclipse-workspace.
-
Refresh the project in the software once to make the changes appear (right-click and select Refresh).
-
Don't forget to replace the 4 variables in the source code that we explained earlier with your own information.
-
After making the changes, right-click on the project name and select Run As > Maven install.
-
The console will display a report indicating the successful completion of the operation.
-
Navigate to the project directory and you will find the generated zip file (.jar) in the target folder. (Please note that the ".jar" file with the larger file size is the main file.)
-
Go to the Amazon and Log into your account and then access the console
-
You can search it in the services section that placed on top of the page.
-
In the Lambda dashboard, click on "Create function button"
-
Choose a desired name for your function and select the Runtime based (for our project Java 8 on Amazon Linux 2)
-
To finalize this new function creation, click the "Create function" button.
-
Your project will be created.
-
Replace the Code source with your own .jar file (upload it from your device).
-
For the correct implementation of the project, it is necessary to enter the main function name in the project in "Handler" field
-
In the Handler field, delete the default name and enter the function name arash.gscfetcher.GSCFetcher::handleRequest
-
You can test your project to ensure it's working properly (consider next 3 steps for testing purpose).
-
Go to the Test tab, choose an arbitrary name for the event and save it. From now on you can hit the Test button whenever you need to test your project
-
If your project is healthy and you've entered the function name correctly, you will see a message indicating successful execution.
-Log output :
-
If there is an issue with the project you've defined, you will encounter an error. In this case, the error will be displayed to you. If you encounter a timeout error, you should perform the next two steps.
-
To configure the time out settings for your function, go to the Configuration tab.
-
In this tab, you can customize various settings for your Lambda function, such as memory allocation, timeout (set time out to 5 minutes), environment variables, execution role, and more.
-
We want this project to run every day so that it can retrieve our Google Console data from a few days ago each day.
-
Select the "EventBridge (CloudWatch Events)" option.
-
In the "Existing rules" section, select the "loop_daily_run" option. Click the "Add" button to save the settings.
-
Finally, your trigger has been created and is displayed in this section.
The GSCFetcher
class implements RequestHandler
to handle AWS Lambda requests. The main method MyGSCFetcherFunction
fetches data from Google Search Console for the past five days and stores it in a MySQL database.
handleRequest
: The entry point for AWS Lambda.MyGSCFetcherFunction
: Fetches data for the past five days.saveToDatabase
: Saves the fetched data to a MySQL database.buildWebmasters
: Builds the Webmasters client using the provided credentials.
To test the code locally, a test unit is provided. This test unit allows you to run and verify the functionality of the code before deploying it to AWS Lambda. The test unit and the pom.xml
file can be downloaded from the GitHub repository.
The test unit provided allows you to run and verify the functionality of the code before deploying it to AWS Lambda.
The pom.xml
file includes the required plugins for compiling the project as a single JAR file and making it compatible with Amazon Lambda.
The provided code fetches data for the past five days as a sample. However, you can customize the date range and other functionalities as per your requirements. For instance, you can modify the MyGSCFetcherFunction
method to fetch data for different date ranges or add more dimensions and filters to the query.
- Invalid Credentials Error: Ensure your Google API credentials are correctly set up and the JSON file is correctly placed.
- Timeout Issues: Increase the timeout setting in the Lambda function configuration.
- Dependency Issues: Ensure all dependencies are correctly specified in the
pom.xml
file and Maven has downloaded them.
For any further assistance, please refer to the official documentation or open an issue on the GitHub repository.