//
// Generic events for tracking events around account creation.
//

// Created account (via email)
$(document).on(
  'submit',
  '.auth-register form, .marketing-signup-modal form, .artist-page-cta-overlay__register form',
  function() {
    $(document).one('ajaxComplete', function(e, xhr, options) {
      var getUrlParameter = function(name) {
        name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
        var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
        var results = regex.exec(location.search);
        return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
      };

      var acquisition_initiative = getUrlParameter('m-id');
      if (_.isEmpty(acquisition_initiative)) {
        acquisition_initiative = getUrlParameter('acquisition_initiative');
      }

      analytics.track('Created account', {
        acquisition_initiative: acquisition_initiative,
        signup_service: 'email',
        user_id: xhr.responseJSON.user.id,
        context: options.context,
        email: xhr.responseJSON.user.email
      });
      analytics.identify( xhr.responseJSON.user.id, {
        email: xhr.responseJSON.user.email
      }, {
        integrations: {
          'All': false,
          'Marketo': true
        }
      });
    })
  }
)

// Created account (via social)

// 1. Upon clicking the social signup button
$(document).on(
  'click',
  '.auth-signup-facebook, .marketing-signup-modal-fb',
  function(e) {
    var getUrlParameter = function(name) {
      name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
      var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
      var results = regex.exec(location.search);
      return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
    };

    var acquisition_initiative = getUrlParameter('m-id');
    if (_.isEmpty(acquisition_initiative)) {
      acquisition_initiative = getUrlParameter('acquisition_initiative');
    }

    // 2. Store some data in cookies before being redirected everywhere
    Cookies.set('analytics-signup', JSON.stringify({
      service: 'facebook',
      acquisition_initiative: acquisition_initiative,
      context: $(e.currentTarget).data('context')
    }))
  }
)

// 3. After landing back on Artsy send the tracking call and expire the cookie
if (Cookies.get('analytics-signup')) {
  var data = JSON.parse(Cookies.get('analytics-signup'))
  Cookies.expire('analytics-signup')
  if (sd.CURRENT_USER) {
    analytics.track('Created account', {
      acquisition_initiative: data.acquisition_initiative,
      signup_service: data.service,
      user_id: sd.CURRENT_USER.id,
      context: data.context
    });
    analytics.identify( sd.CURRENT_USER.id, {
      email: sd.CURRENT_USER.email
    }, {
      integrations: {
        'All': false,
        'Marketo': true
      }
    });
  }
}

analyticsHooks.on('auth:login', function (options) {
  analytics.track('Successfully logged in')
})

// Triggered sign up form via save button
if (!sd.CURRENT_USER) {
  $('.artwork-item-image-container .overlay-button-save').click(function () {
    analytics.track('Triggered sign up form via save button')
  })
}

// Triggered sign up form via follow button
if (!sd.CURRENT_USER) {
  $('.follow-button, .entity-follow').click(function () {
    analytics.track('Triggered sign up form via follow button')
  })
}

// Clicked sign up via the header
$('.mlh-signup').click(function () {
  analytics.track('Clicked sign up via the header')
})

// Clicked sign out via the header
$('.mlh-logout').click(function () {
  analytics.track('Clicked logout via the header')
})

// Viewed sign up options
var trackViewSignup = function () {
  analytics.track('Viewed sign up options')
}

analyticsHooks.on('mediator:open:auth', function (options) {
  if (options.mode === 'signup') trackViewSignup()

  analytics.trackLink($('.auth-signup-facebook')[0], 'Created account')
  analytics.trackLink($('.auth-signup-twitter')[0], 'Created account')
})

$('#auth-footer [href*=sign_up]').click(trackViewSignup)

@@ -67,293 +67,42 @@
 //%0A%0A
-// Created account (via email)%0A$(document).on(%0A  'submit',%0A  '.auth-register form, .marketing-signup-modal form, .artist-page-cta-overlay__register form',%0A  function() %7B%0A    $(document).one('ajaxComplete', function(e, xhr, options) %7B%0A      var getUrlParameter = function(name) %7B%0A      
+const getUrlParameter = name =%3E %7B%0A
   na
@@ -160,25 +160,19 @@
 %5C%5D');%0A  
-      var
+let
  regex =
@@ -217,25 +217,19 @@
 *)');%0A  
-      var
+let
  results
@@ -260,22 +260,16 @@
 earch);%0A
-      
   return
@@ -280,34 +280,41 @@
 ults === null ? 
-''
+undefined
  : decodeURIComp
@@ -356,17 +356,244 @@
 ));%0A
-      %7D;%0A
+%7D%0A%0A// Created account (via email)%0A$(document).on(%0A  'submit',%0A  '.auth-register form, .marketing-signup-modal form, .artist-page-cta-overlay__register form',%0A  function() %7B%0A    $(document).one('ajaxComplete', function(e, xhr, options) %7B
 %0A   
@@ -1439,323 +1439,8 @@
 ) %7B%0A
-    var getUrlParameter = function(name) %7B%0A      name = name.replace(/%5B%5C%5B%5D/, '%5C%5C%5B').replace(/%5B%5C%5D%5D/, '%5C%5C%5D');%0A      var regex = new RegExp('%5B%5C%5C?&%5D' + name + '=(%5B%5E&#%5D*)');%0A      var results = regex.exec(location.search);%0A      return results === null ? '' : decodeURIComponent(results%5B1%5D.replace(/%5C+/g, ' '));%0A    %7D;%0A%0A
     

Consolidate the getUrlParameter function