Email Alerts for new Referers in Google Analytics using Apps Script
It's useful to know when you have a new website referrer. Google Analytics is plagued with spam referral and you want to filter this out of reporting as quickly as possible to stop it from skewing your data. It's also helpful to be able to respond quickly to new referral traffic - maybe leave a comment or promote the new link on social media.
The script below will send you a daily email with links to any new referrers (this is GA3, there is a GA4 version later in this post).
Start a new apps script project in Google Drive and paste in the code. At the top enter the view ID that you want to monitor and the email address that should receive reports.
Choose Advanced Google Services from the Resources menu and switch on the Google Analytics API. Then click the Google API Console link and enable the Google Analytics API there as well.
Finally pick Current project's triggers from the Edit menu and trigger the main function daily at a convenient time.
This script saves known referrers in script properties. For a site with lots of traffic this may run out of space in which case you might need to switch this out and write known referrers to a sheet instead.
For Google Analytics 4 properties use the version of the script below. The setup process is the same, but you need the Google Analytics Data API instead of the Google Analytics API.
More Google Apps Script Projects
- Get an email when your security camera sees something new (Apps Script + Cloud Vision)
- Get an email if your site stops being mobile friendly (no longer available)
- Export Google Fit Daily Steps, Weight and Distance to a Google Sheet
- Animation of a year of Global Cloud Cover
- Control LIFX WiFi light bulbs from Google Apps Script
- How to backup Google Photos to Google Drive automatically after July 2019 with Apps Script
- Using the Todoist API to set a due date on the Alexa integration to-do list (with Apps Script)
- Automate Google PageSpeed Insights and Core Web Vitals Logging with Apps Script
- Using the Azure Monitor REST API from Google Apps Script
- Monitor page index status with Google Sheets, Apps Script and the Google Search Console API
(Published to the Fediverse as: Email Alerts for new Referers in Google Analytics using Apps Script #code #googleanalytics #appsscript #gas #ga4 Apps script that will email you any new referral traffic from Google Analytics. Useful for responding to new links and referrer spam. GA3 and GA4 versions. )
BlogEngine.NET most popular pages widget using Google Analytics
I finished off my BlogEngine.NET migration yesterday missing a couple of useful sections from the previous incarnation of this blog. The first is a list of the most popular posts based on Google Analytics data. I've just finished porting this from a UserControl to a widget for BlogEngine.NET. To use this just download and extract this zip file to your widgets directory:
MostPopularFromGA.zip (5.22 kb)
You can see the widget in action under the Most Popular heading to the left if you're reading this post on the blog.
Most of the settings should be pretty obvious. The Google Analytics profile is the exception. This isn't the ID included in your tracking code. To get the profile log in to Google Analytics and click Edit next to the profile you want to use. At the top of the page you'll see a Profile ID. You need to use this number prefixed by ga: (i.e. ga:1234567). Once you have this and your account credentials entered you should be up and running.
The Post must match settings is a regular expression used to filter the Google Analytics report to only include blog posts. The default value corresponds to a default BlogEngine.NET install and only includes pages that start .../post/ (the regular expression is ^/post/). If your posts are under .../blog/posts/ then just update accordingly (^/blog/posts/). If you want to include the most popular pages regardless of the path just leave this setting blank.
If you have any questions or feature requests leave a comment below.
Related Posts
- Migrating from Blogger to BlogEngine.NET
- Geotagging posts in BlogEngine.NET
- Migrating a C# Integration from GA3 to GA4
You Might Also Like
- Better related posts with word2vec (C#)
- Twin Peaks
- Congress: instead of breaking the Internet how about fixing child identity theft?
(Published to the Fediverse as: BlogEngine.NET most popular pages widget using Google Analytics #code #blogengine.net #googleanalytics A BlogEngine.NET most popular posts widget using data from the Google Analytics API. )