DEV Community

Clozzi
Clozzi

Posted on Edited on

Find Keyword Cannibalization in Your Ecommerce Store With Python

If you build or maintain an online store, you have probably seen this problem: two pages on the same site compete for the same search query, and neither ranks well. This is called keyword cannibalization, and it is easy to introduce by accident when a catalog grows.

The good news is that you can catch it automatically. In this post we will build a small Python script that reads a keyword-to-URL mapping and flags conflicts. I will use a medical apparel store as the example, since its catalog has a clear page hierarchy: a homepage, plus category pages for men, women, coats and caps.

The Idea: One Primary Keyword per URL

Every important page should own one primary keyword, plus a few close variants. If two URLs claim the same keyword, search engines have to guess which one to show.

Broad terms belong on the homepage. For example, a store aimed at medical scrubs online would target that location-based query on its homepage, because it describes the whole catalog. Narrower product terms belong on category pages, such as a lab coat collection for coat-related queries.

A clean mapping looks like this:

Page Primary keyword Secondary examples
Homepage medical scrubs scrubs, scrub suit
Lab coats lab coat white coat, doctor coat
Men's scrubs mens scrubs scrubs for men
Women's scrubs scrubs for women ladies scrubs
Caps surgical caps unisex scrub caps

Doing this by eye works for 20 keywords. It does not work for 500, which is where a script helps.

The Input File

Most keyword tools export a spreadsheet. Save yours as a tab-separated file with these columns:

Keyword URL Cluster Volume KD
scrubs https://example.com/ homepage 8100 50
lab coat https://example.com/collections/lab-coats Labcoats 1900

white coat https://example.com/collections/lab-coats Labcoats 720

The Script

The tricky part is that "scrub suit" and "suit scrub", or "scrub" and "scrubs", are effectively the same query. So we normalize each keyword before comparing: lowercase it, strip a trailing "s" from longer words, and sort the words.

python
import csv
from collections import defaultdict

def normalize(keyword):
"""Reduce a keyword to a comparable form: lowercase, singular, sorted."""
words = keyword.lower().strip().split()
words = [w[:-1] if w.endswith("s") and len(w) > 3 else w for w in words]
return " ".join(sorted(words))

def find_conflicts(path):
groups = defaultdict(list)

with open(path, newline="", encoding="utf-8") as f:
    for row in csv.DictReader(f, delimiter="\t"):
        keyword = (row.get("Keyword") or "").strip()
        url = (row.get("URL") or "").strip()
        if not keyword or not url:
            continue  # skip unmapped keywords
        groups[normalize(keyword)].append((keyword, url))

# A conflict is one normalized keyword mapped to more than one URL
return {
    key: items
    for key, items in groups.items()
    if len({url for _, url in items}) > 1
}
Enter fullscreen mode Exit fullscreen mode

if name == "main":
conflicts = find_conflicts("keywords.tsv")

if not conflicts:
    print("No cannibalization found.")
else:
    for key, items in conflicts.items():
        print(f"\nConflict: '{key}'")
        for keyword, url in items:
            print(f"  - {keyword!r} -> {url}")
Enter fullscreen mode Exit fullscreen mode

Run it with python check_keywords.py. If two pages target the same normalized keyword, you get output like this:

Conflict: 'coat white'

That tells you exactly which pages to fix: merge them, or give each a different primary keyword.

Extending the Script

A few easy upgrades:

Report unmapped keywords. Collect rows with a keyword but no URL, and print them as a to-do list.
Use search volume. Sort conflicts by the Volume column so you fix the highest-traffic overlaps first.
Catch partial overlaps. Compare word sets with Jaccard similarity to flag near-duplicates like "mens scrubs" and "best scrubs for men".
Run it in CI. Fail the build when a new keyword creates a conflict, so the problem never reaches production.

On-Page Checklist for Each URL

Once the mapping is clean, check each page:

The primary keyword is in the title and the first 100 words
There is one H1, with H2s covering related sub-topics
Product images have descriptive alt text
The meta description is unique and under 160 characters
The page links to one or two relevant pages, not twenty

Common Mistakes

Targeting the same keyword on the homepage and a category page
Stuffing keywords into product descriptions
Ignoring low-volume long-tail terms with high intent
Forgetting to re-run the check when you add a new collection
Wrapping Up

Keyword mapping is a spreadsheet and a few rules, and a short script keeps those rules honest as your catalog grows. Go broad on the homepage, specific on categories, and keep one primary keyword per URL.

If you have a different way to detect cannibalization, or an improvement to the script, share it in the comments.

Top comments (0)