Page MenuHomePhabricator

ClickThroughRateInstrument v1: Handle multiple elements gracefully
Open, LowPublic

Description

Background

Currently, CTR v1 only uses the first element yielded by the selector. The Moderator Tools team immediately invalidated this assumption by querying whether it could be used with multiple elements on a page, e.g. the diff links on the Special:RecentChanges page.

AC

  • ClickThroughRateInstrument.start() and .stop() still handle the single element case
  • ClickThroughRateInstrument.startMany() and .stopMany() handle the multiple element case

Event Timeline

Wait, I'm confused. I was under the impression that we decided that the CTR instrument v1 should be simple and for a single element (first one returned by document.querySelector), so if a developer needed to attach a CTR instrument to every element in a collection, they had to do it explicitly – not provide a collection of elements to the CTR instrument to keep track off. And that a single element case was not a collection-of-length-1, but the only case the instrument handles.

I think if you wanted to make it easier for developers to accomplish the task, perhaps a wrapper that replicates this pattern

$( 'a.ext-campaignevents-events-list-link' ).each( function( index, element ) {
    ClickThroughRateInstrument.start(
        'a.ext-campaignevents-events-list-link:nth-of-type(' + (index + 1) + ')',
        'Collaboration list event link'
    );
} );

Wait, I'm confused. I was under the impression that we decided that the CTR instrument v1 should be simple and for a single element (first one returned by document.querySelector), so if a developer needed to attach a CTR instrument to every element in a collection, they had to do it explicitly – not provide a collection of elements to the CTR instrument to keep track off.

You're right. I had gotten myself in a muddle about ClickThroughRateInstrument.start() and .stop(). That said, I think having a ClickThroughRateInstrument.startMany() method would:

  1. Keep the logic around "What does it mean to start an instrument" within the implementation of the instrument
  2. Provide a good name for what's going on
  3. Help folks keep their code DRY