samvera-labs / hyrax-google_analytics

0 stars 0 forks source link

Do not hardcode Hyrax DOM ids into plugin JS event handlers #5

Open afred opened 7 years ago

afred commented 7 years ago

E.g.

$(document).on('click', '#file_download', function(e) {
  _gaq.push(['_trackEvent', 'Files', 'Downloaded', $(this).data('label')]);
});

Get rid of the '#file_download' part.

One way to do it might be like this...

$(document).on('click', Hyrax.config.file_download_id, function(e) {
  _gaq.push(['_trackEvent', 'Files', 'Downloaded', $(this).data('label')]);
});

done when: no '#file_download' is mentioned specically in the event handler - OR - it is parameterized, such as above.

cbeer commented 7 years ago

Or, define some additional data attributes that can be used consistently to trigger this counter.

afred commented 7 years ago

@projecthydra-labs/hydra_plugins_wg I like @cbeer's suggestion here. Defining a known set of data attributes as the interface to attach events to seems more straightforward and more conventional to me. Also, less work :)

botimer commented 7 years ago

Yes, exactly. It's all about making a statement of what can be relied upon. Poking at IDs as they just happen to be used is scary business.

For some cases, it might be config-style stuff in a global. For some, it might be an ID. For some, it might be a class and some markup following a documented convention.