Not every Python script needs to be complex, long, and work of art. Sometimes it can help with quick mundane tasks. One such opportunity presented itself a couple of weeks ago where a client asked for 50+ IPs to be filtered from a Google Analytics view.
We could have taken 20 minutes and manually added them, but we instead took 5 minutes to write a very small Python script to do it for us and now we have a script to use next time a client wants to bulk exclude filter IPs too! Let’s get started!
Table of Contents
Requirements and Assumptions
- Python 3 is installed and basic Python syntax understood
- Access to a Linux installation (I recommend Ubuntu) or Google Colab.
- IP list in a CSV under column name “ip”
Import and Install Modules
- pandas: for importing the IPs
Let’s get that one lonely module imported.
import pandas as pd
Next, we import the CSV containing our IPs into a dataframe.
Import and Convert
df = pd.read_csv("ipp-ips.csv")
Now we convert the IP column into a list object.
ip_list = df["ip"].tolist()
Escape and Join
With a couple of simple list comprehensions, we escape the period (.) in each IP, which is RegEx signifies a wild card and we add in the start and end anchor (^ and $) to match the IPs exactly. The start and end anchors express how a string should begin and end exactly.
ip_list = [x.replace('.','\.') for x in ip_list ] ip_list = ['^' + x + '$' for x in ip_list ]
Once we have our IPs formatted we need to add in the RegEx alternation pipe between each of them which signifies “OR”.
seperator = "|" print(seperator.join(ip_list))
Add to Google Analytics Filter
Run the script and you could get an output like (depending on how many IPs you have, yes I know these are all the same IP):
Head over to your Google Analytics view and paste into the filter creation. Note there is a 255 character limit per filter. If you have many IPs, you’ll need to split them up into multiple filters. I would love to learn a nice way of doing this automatically with Python while not cutting the IPs up in bad spots.
In seconds you avoid the manual grind of adding IPs to a Google Analytics filter. Remember to try and make my code even more efficient. Now get out there and try it out! Follow me on Twitter and let me know your applications and ideas!
- Build and Run Python Scripts on the Fly With GPT-3 - January 5, 2023
- Compare Keyword SERP Similarity in Bulk with Python - November 18, 2022
- Analyze SERP Backlink Profiles in Bulk for SEO Using Python - September 10, 2022