Arthur Camberlein >> SEO & Data articles >> How to change tag style in bulk? For an experiment

How to change tag style in bulk? For an experiment

Written by Arthur Camberlein | Published on & updated on

Not so long ago, I faced an issue while wanted to test a colleague idea: how can we test on which links user are more willing to click.

TL;DR

If my article is too long, these are the steps:

  1. Define the scope
  2. Extract or get the HTML content
  3. Split your group (in three in my case) 
  4. Write a script
  5. Run the script
  6. Push your changes

And this is the script I use for the curious: The Script

The process

 So the first step was to define the scope of your changes. In my case it was for an experiment, we wanted to test the impact of style on links from blogs posts.

I'll let you ran some test and opportunity sizing to define how many articles and for how long you should run the test [tip: it will depend on how many sessions / user interaction you have]. For this article, let's say 500 blog posts for a two weeks period of time.

Out of these 500 articles, I extract 100 for the first variant, 100 for the second variant and 300 for the control group. [Remember that numbers may change based of your data and your goal!]

Why creating a script: because of sample for both variants was over a hundred blog posts each. No way we would have done this manually.

The script

For this purpose, I used Python that was the most adapted to the situation & for the user to easily reproduce. But if you are more “fluent” in R, PHP, Ruby, JS, you might be able to adapt it 😁.

import pandas as pd
from bs4 import BeautifulSoup

# Function to add bold red style to links
def bold_red(body_html):
    soup = BeautifulSoup(body_html, 'html.parser')
    for a in soup.find_all('a', href=True):
        a['style'] = "color: #880808; font-weight: bold;"
    return str(soup)

# Function to add italic underline black style to links
def italic_underline_black(body_html):
    soup = BeautifulSoup(body_html, 'html.parser')
    for a in soup.find_all('a', href=True):
        a['style'] = "color: #000000; font-style: italic; text-decoration: underline;"
    return str(soup)
  
variant1['new_body_html'] = variant1['body_html'].apply(bold_red)

variant2['new_body_html'] = variant2['body_html'].apply(italic_underline_black)

Let me explain this script a little! First, I am creating two functions that I could apply to the two selected group. These two functions leverage BeautifulSoup to achieve the search and replace. And finally, I am using apply to make changes in bulk.

And then?

This was not the end of the script and< the automation. If you know me a bit, I like to automate and simplify process. The other part of the script (that I am not able to share) aimed to ship these changes in bulk.

Yes, once the HTML is modified, the next logical step is to ship those changes and run the experiment. I am sure if you are using a CMS you can easily achieve that.


If you want some result, ping me on X (formerly Twitter), LinkedIn or Bluesky. And if many of you are interested in this test, I'll create a recap article.

Back to blog

Learn more with the article FAQ

How to change tag style in bulk? For an experiment - FAQs

How to Conduct A/B Testing on Blog Links?

Discover a step-by-step guide on conducting A/B testing on blog links to understand user behavior better. The process involves defining the scope, extracting HTML content, splitting your group, writing a script, running the script, and pushing your changes. The guide also includes a Python script that uses BeautifulSoup to modify the HTML content of the links, enabling you to test different styles and their impact on user interaction.

How to Automate A/B Testing on Blog Links Using Python?

Learn about the automation of A/B testing on blog links using Python. The process involves creating a script that modifies the HTML content of the links in bulk, allowing you to test different styles on a large number of blog posts. The script uses BeautifulSoup to search and replace the link styles, and the changes are then shipped in bulk for testing.

What is the Process of Running an Experiment on Blog Link Styles?

Explore the process of running an experiment to test the impact of different styles on blog links. The process involves defining the scope of the experiment, extracting the HTML content, splitting the group into variants and a control group, and writing and running a Python script to modify the link styles. The changes are then shipped in bulk, and the experiment is run to determine which style users are more likely to click.

Blog post taggued in: Automation, Data, Learning, Python, Python SEO

Written by