From c6e67044dd13df4898179cf790a117c762fb90ff Mon Sep 17 00:00:00 2001
From: Paul Kaplan <pkaplan@media.mit.edu>
Date: Thu, 19 Jul 2018 11:29:44 -0400
Subject: [PATCH] Add library analytics for searches

---
 src/components/library/library.jsx | 14 ++++++++++++--
 src/containers/tips-library.jsx    |  1 +
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/src/components/library/library.jsx b/src/components/library/library.jsx
index c58d3608c..2da09f272 100644
--- a/src/components/library/library.jsx
+++ b/src/components/library/library.jsx
@@ -9,6 +9,7 @@ import Modal from '../../containers/modal.jsx';
 import Divider from '../divider/divider.jsx';
 import Filter from '../filter/filter.jsx';
 import TagButton from '../../containers/tag-button.jsx';
+import analytics from '../../lib/analytics';
 
 import styles from './library.css';
 
@@ -28,6 +29,7 @@ class LibraryComponent extends React.Component {
         super(props);
         bindAll(this, [
             'handleBlur',
+            'handleClose',
             'handleFilterChange',
             'handleFilterClear',
             'handleFocus',
@@ -56,9 +58,17 @@ class LibraryComponent extends React.Component {
         this.handleMouseEnter(id);
     }
     handleSelect (id) {
-        this.props.onRequestClose();
+        this.handleClose();
         this.props.onItemSelected(this.getFilteredData()[id]);
     }
+    handleClose () {
+        analytics.event({
+            category: 'library',
+            action: `${this.props.id}: close with search`,
+            label: this.state.filterQuery || '(empty)'
+        });
+        this.props.onRequestClose();
+    }
     handleTagClick (tag) {
         this.setState({
             filterQuery: '',
@@ -111,7 +121,7 @@ class LibraryComponent extends React.Component {
                 fullScreen
                 contentLabel={this.props.title}
                 id={this.props.id}
-                onRequestClose={this.props.onRequestClose}
+                onRequestClose={this.handleClose}
             >
                 {(this.props.filterable || this.props.tags) && (
                     <div className={styles.filterBar}>
diff --git a/src/containers/tips-library.jsx b/src/containers/tips-library.jsx
index ebbee3f30..009c8c2fa 100644
--- a/src/containers/tips-library.jsx
+++ b/src/containers/tips-library.jsx
@@ -54,6 +54,7 @@ class TipsLibrary extends React.PureComponent {
             <LibraryComponent
                 data={decksLibraryThumbnailData}
                 filterable={false}
+                id="tipsLibrary"
                 title={this.props.intl.formatMessage(messages.tipsLibraryTitle)}
                 visible={this.props.visible}
                 onItemSelected={this.handleItemSelect}
-- 
GitLab