Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Count choices in an ACBC on the fly

Hi,
I have a client that wants to count how often a brand is chosen in the screener section and the tournament an ACBC. I know you can use ACBC functions to identify a winner in the tournament or the BYO, but this is something they want as a count (sum) for each of the brand levels. We'd want to create a count variable for each brand, then pass that variable from ACBC hosted on the Sawtooth server to the main survey, which is hosted externally. But we have to get there first....

I have the feeling this is possible with PERL script, but I can't quite see my way to this. Any ideas?

Thanks,
Stuart Drucker
Drucker Analytics, Inc.
asked Apr 9, 2018 by sdrucker (180 points)

1 Answer

0 votes
Start by creating a free format question on the same page as the first Screener task.  Give it a name that ends with a 1, like "acbcff1."

Add one hidden variable to the free format question for each brand.  Name the variables "_brand1", "_brand2", etc.

Set the free format's HTML to this:

<input name="[% QuestionName() %]_brand1" id="[% QuestionName() %]_brand1" class="brandCount" type="hidden" value=""/>
<input name="[% QuestionName() %]_brand2" id="[% QuestionName() %]_brand2" class="brandCount" type="hidden" value=""/>
<input name="[% QuestionName() %]_brand3" id="[% QuestionName() %]_brand3" class="brandCount" type="hidden" value=""/>
<input name="[% QuestionName() %]_brand4" id="[% QuestionName() %]_brand4" class="brandCount" type="hidden" value=""/>

<script>
function SSI_CustomGraphicalRadiobox(graphical, input) {
    if ($('.question.acbc_choicetask').length) {
        $('.brandCount').val(0);
        var choice = $('.question.acbc_choicetask').attr('id').replace(/_div/, '');
        var match = input.id.match(/_ChoiceTask[0-9]+_([0-9]+)$/);
        if (match) {
            var td = Number(match[1]) + 1;
            var brand = $('#' + choice + '_div .inner_table > tbody > tr > td:nth-child(' + td + ') .acbcBrand').val();
            $('#[% QuestionName() %]_brand' + brand).val(1);
        }
    }
}
</script>


Copy-and-paste lines 1 through 4 as needed so you have one input for each free format variable.

Now give the free format this custom JavaScript verification:

if ($('.question.acbc_screener').length) {
    var screener = $('.question.acbc_screener').attr('id').replace(/_div/, '');
    $('.brandCount').val(0);
    var concepts = $('#' + screener + '_div input[type=radio]').length / 2;
    for (var concept = 1; concept <= concepts; concept++) {
        if (SSI_GetValue(screener + '_' + concept)) {
            var brand = $('#' + screener + '_div .inner_table > tbody > tr > td:nth-child(' + (concept + 1) + ') .acbcBrand').val();
            var count = Number($('#[% QuestionName() %]_brand' + brand).val());
            $('#[% QuestionName() %]_brand' + brand).val(count + 1);
        }
    }
}


Next, copy-and-paste this free format question so that there exists one of it on each page with a Screener or Choice task.

Last, add one more free format question on a page after all the Screener and Choice tasks.  Give this last free format question the same variables as you did before.  Give it this HTML:

[% Begin Unverified Perl
my $ffBaseName = 'acbcff';
my $ffNumberOfQuestions = 18;
my $numberOfBrands = 4;

my $output = '';
for (my $brand = 1; $brand <= $numberOfBrands; $brand++) {
    my $total = 0;
    for (my $question = 1; $question <= $ffNumberOfQuestions; $question++) {
        $total += GETVALUE($ffBaseName . $question . '_brand' . $brand);
    }
    $output .= '<input name="' . QUESTIONNAME() . '_brand' . $brand . '" id="' . QUESTIONNAME() . '_brand' . $brand . '" type="hidden" value="' . $total . '"/>';
}
return $output;
End Unverified %]


Line 2 must be updated with the base name of the earlier free format questions.  Line 3 must be updated with the total number of previous free format questions.  Line 4 must be updated with the total number of brands.
answered Apr 10, 2018 by Zachary Platinum Sawtooth Software, Inc. (205,575 points)
...