If you're looking to compile a list of user agents and bots visiting your website without requiring developer assistance or additional server costs, this article will guide you through the process.
Steps
1. Create Google Sheet
This will act as your sheet to store your user agents over time.
- Create a Column: Create a new Google Sheet and add a header to column A, "Users"
- Note Name of Sheet: By default, keep it "Sheet1." If it isnt, change to Sheet1
2. Create Apps Script
This will act as the script needed to write to your sheet.
- Access Apps Script: In your Google Sheet, go to the top menu and click on "Extensions" -> "Apps Script."
- Edit Code.gs: In the script editor, replace the existing code with the content from this repository's Code.gs.
- Deploy: After editing, set the project title to "User-Agents" and click on "Deploy" -> "New Deployment."
3. Set Permissions and Publish
- Deployment Type: Make sure click on the cog "settings" icon and deploy as a "Web app."
- Authorize Access: Make sure to share your Google sheet access
-
Copy Web App URL: After deploying, you'll get a Web App URL like
https://script.google.com/macros/s/xxxxxxxxxxxxxx/exec
. This is your API endpoint.
4. Setup Google Tag Manager
This read your user agent, store it in a variable and then send it to your google sheet (but not store duplicates if they already exist)
Setup Variable: Create New Variable called "JavaScript User Agent Variable" and configure Variable Type = Javascript Variable -> Global Variable Name = navigator.userAgent
Setup Tag: Create New Tag called "Update User Agent Sheet with API" and configure Tag Type as below (make sure to replace url or xxxxxxxxxxx with the url you copied in above step: "Copy Web App URL" as well as setup triggering for "all pages":
<script>
fetch('https://script.google.com/macros/s/xxxxxxxxxxxxx/exec?path=Sheet1&action=write&Users={{JavaScript User Agent Variable}}')
.then(function(res) {
return res.json();
})
.then(function(data) {
console.log(data);
});
</script>
Publish Tag: Publish and Name it.
5. Success
Congratulations! If you followed steps, you can check back in next few days and see a growing list of User Agents in your sheet.
Important Notes
- Limitations: This free solution uses Google Sheets and makes it a public API. This means that anyone that has the url of this sheet can access it. Make sure you are aware of what this means and use it at your own risk.
- Duplicate Checks: The script will check for duplicates before appending a new row. If a duplicate is found, it will not add the new entry.
- Access and Limitations: Make sure to read about sharing settings in Google Sheets and familiarize yourself with Google Sheets and Apps Scripts guidelines, quotas, and limitations.
- Disclaimer: All code and instructions are provided "as is." By using this guide, you agree that you are solely responsible for your actions.