//
// Analytics for all things articles. This includes tests for share buttons
// and potentionally other alternative layout options or more.
//

if (location.pathname.match('/article/') || location.pathname.match('/year-in-art-2016')) {
  $(document.body).on('click', '.article-social a', function () {
    var articleId = $(this).closest('.article-container').data('id')
    analytics.track('Article Share', {
      article_id: articleId,
      context_type: sd.ARTICLE ? 'article_fixed' : 'magazine_fixed',
      service: $(this).attr('data-service')
    })
  }).on('click', '.article-share-fixed > a', function () {
    var articleId = $(this).closest('.article-container').data('id')
    analytics.track('Article Share', {
      article_id: articleId,
      context_type: 'article_sticky',
      service: $(this).attr('data-service')
    })
  }).on('click', '.article-section-toc-link a', function () {
    var articleId = $(this).closest('.article-container').data('id')
    analytics.track('Clicked article impression', {
      article_id: articleId,
      destination_path: null,
      impression_type: 'toc',
      context_type: 'article_fixed'
    })
  }).on('click', '.article-section-image-set', function () {
    var articleId = $(this).closest('.article-container').data('id')
    analytics.track('Clicked article impression', {
      article_id: articleId,
      destination_path: null,
      impression_type: 'image_set',
      context_type: 'article_fixed'
    })
  }).on('click', '.article-section-callout', function () {
    var articleId = $(this).closest('.article-container').data('id')
    // Only track callouts that are links
    if ($(this)[0].href) {
      analytics.track('Clicked article impression', {
        article_id: articleId,
        destination_path: $(this)[0].href.replace(/^.*\/\/[^\/]+/, ''),
        impression_type: 'article_callout',
        context_type: 'article_fixed'
      })
    }
  }).on('click', '.article-related-widget a', function () {
    var articleId = $(this).closest('.article-related-widget').data('id')
    analytics.track('Clicked article impression', {
      article_id: articleId,
      destination_path: $(this)[0].href.replace(/^.*\/\/[^\/]+/, ''),
      impression_type: 'related_article',
      context_type: 'article_fixed'
    })
  }).on('click', '.article-sa-primary-logo a', function(){
    analytics.track('Clicked primary partner logo', {
      destination_path: $(this)[0].href.replace(/^.*\/\/[^\/]+/, ''),
      impression_type: 'sa_primary_logo',
      context_type: 'article_fixed'
    })
  }).on('click', '.article-sa-secondary-logo a', function(){
    analytics.track('Clicked secondary partner logo', {
      destination_path: $(this)[0].href.replace(/^.*\/\/[^\/]+/, ''),
      impression_type: 'sa_secondary_logo',
      context_type: 'article_fixed'
    })
  }).on('click', '.article-sa-cta-container a', function(){
    analytics.track('Clicked partner cta link', {
      destination_path: $(this)[0].href.replace(/^.*\/\/[^\/]+/, ''),
      impression_type: 'sa_partner_cta',
      context_type: 'article_fixed'
    })
  })

  // Hooks
  analyticsHooks.on('readmore', function (options) {
    analytics.track('Clicked Read More', {})
    analytics.page({path: location.pathname})
    if (window.PARSELY) {
      window.PARSELY.beacon.trackPageView({
        url: location.href,
        urlref: sd.APP_URL + '/' + options.urlref,
        js: 1,
        action_name: 'infinite'
      })
    }
    if (window.Sailthru) {
      window.Sailthru.track({
        domain: 'horizon.artsy.net',
        spider: true,
        track_url: true,
        url: sd.APP_URL + '/' + location.pathname,
        use_stored_tags: true
      })
    }
  })

  analyticsHooks.on('view:editorial-signup', function (options) {
    analytics.track('Article impression', {
      article_id: null,
      destination_path: null,
      impression_type: 'newsletter_signup',
      context_type: options.type
    }, { integrations: { 'Mixpanel': false } })
  })
}

// Applies to both /article/* and /articles
if (location.pathname.match('/article/') || location.pathname.match('/articles') || location.pathname.match('/gallery-insights')) {
  analyticsHooks.on('submit:editorial-signup', function (options) {
    analytics.track('Sign up for editorial email', {
      article_id: $(this).closest('.article-container').data('id'),
      context_type: options.type,
      user_email: options.email
    })
  })

  analyticsHooks.on('submit:gi-signup', function (options) {
    analytics.track('Sign up for gallery insights email', {
      article_id: $(this).closest('.article-container').data('id'),
      context_type: 'article_fixed',
      user_email: options.email
    })
  })

  analyticsHooks.on('dismiss:editorial-signup', function (options) {
    analytics.track('Dismiss editorial signup', {
      context_type: options.type
    })
  })
}

@@ -209,16 +209,21 @@
 match('/
+2016-
 year-in-
@@ -229,13 +229,8 @@
 -art
--2016
 ')) 

Fix one path