oTree Forum >

Issue with Firm URLs Opening Incorrectly

#1 by galbitton

Dear oTree Community,

I'm currently working on an experiment using oTree, and I've encountered an issue that I could use some assistance with. Let me provide some context and details about my experiment:


Experiment Overview:
- In my study, participants are presented with information about four different firms.
- I've provided participants with four URL links, each pointing to the homepage of one of these firms.
- Participants are divided into control and treatment groups, and they are presented with four firms in a random order. To be more specific, I have a pool of 12 firms and their URLs (4 of them are for the control group only) and 8 are randomly assigned to treatment. 
- To make the experiment user-friendly, I've given nicknames to these URLs so that participants see the firm names they need to click on, rather than the actual URL.

HTML Code Snippet:

<div class="links-section">
    <ul>
        <li><a href="{{ websites.0 }}" target="_blank">{{ player.firm1_name }}</a></li>
        <li><a href="{{ websites.1 }}" target="_blank">{{ player.firm2_name }}</a></li>
        <li><a href="{{ websites.2 }}" target="_blank">{{ player.firm3_name }}</a></li>
        <li><a href="{{ websites.3 }}" target="_blank">{{ player.firm4_name }}</a></li>
    </ul>
</div>

init.py Code Snippet: 

def creating_session(self):
    control_websites = [
        'https://A', #I changed on purpose the real URLs
        'https://B',
        'https://C',
        'https://D'
    ]

    high_risk_websites_treatment = [
        'https://1',
        'https://2',
        'https://3',
        'https://4'
    ]

    low_risk_websites_treatment = [
        'https://5',
        'https://6',
        'https://7',
        'https://8'
    ]
    
        for player in self.get_players():
        import random
        player.participant.vars['dropout'] = False
        if random.choice([True, False]):  
            player.participant.vars['group'] = 'treatment'
            player.treatment_group = 'treatment'
            selected_high_risk = random.sample(high_risk_websites_treatment, 2)
            selected_low_risk = random.sample(low_risk_websites_treatment, 2)
            player.participant.vars['websites'] = selected_high_risk + selected_low_risk
        else:
            player.participant.vars['group'] = 'control'
            player.treatment_group = 'control'
            random.shuffle(control_websites)  
            player.participant.vars['websites'] = control_websites

        for i, website in enumerate(player.participant.vars['websites']):
            firm_name, _ = firm_names.get(website, ("", ""))
            setattr(player, f'firm{i + 1}_name', firm_name)

class Instructions(Page):
    form_model = 'player'

    def vars_for_template(self):
        return {
            'websites': self.participant.vars.get('websites', [])
        }

The Issue:
The problem I'm encountering is that when participants in the control group (the treatment group is fine) click on one firm's link, it opens the URL of a different firm. In other words, the URLs appear to be mixed up, even though the nicknames displayed are correct. What's particularly perplexing is that this issue seems to manifest itself exclusively when I create a new session with a designated room or without any room, but curiously, it doesn't occur when I utilize the Demo feature.

If you need more specific details or code snippets, please let me know, and I'll be happy to provide them. Thank you in advance for your help and support.

Best,
Gal

#2 by galbitton

I've finally resolved the issue and would like to share the solution, which could potentially benefit others. Basically, I changed "vars_for_template" to: 

    def vars_for_template(self):
        websites = self.participant.vars.get('websites', [])
        firm_names_dict = {website: firm_names.get(website, ("", ""))[0] for website in websites}

        return {
            'websites_and_firm_names': firm_names_dict.items(),
        }
        
and in the HTML page I used: 

        <ul>
        {% for website, firm_name in websites_and_firm_names %}
            <li>
                <a href="{{ website }}" target="_blank">{{ firm_name }}</a>
            </li>
        {% endfor %}
        </ul>

Write a reply

Set forum username