/* global Core */ var Core = window.Core || {}; Core.Drip = Core.Drip || ( function ( document, window, $ ) { 'use strict'; /** * Private functions and properties. * * @since 1.0.0 * * @type {Object} */ var __private = { /** * Validate email relying only on browser checks. * * @see https://stackoverflow.com/a/13975255/515162 * * @since 1.0.0 * * @param value String to check whether this is a valid email address. * * @return boolean */ isEmailValid: function ( value ) { var input = document.createElement( 'input' ); input.type = 'email'; input.required = true; input.value = value; return typeof input.checkValidity === 'function' ? input.checkValidity() : /\S+@\S+\.\S+/.test( value ); } }; /** * Public functions and properties. * * @since 1.0.0 * * @type {Object} */ var app = { visitor_uuid: '', userCity: '', userCountry: '', /** * Start the engine. DOM is not ready yet, use only to init something. * * @since 1.0.0 */ init: function () { // Do that when DOM is ready. $( document ).ready( app.ready ); }, /** * DOM is fully loaded. * * @since 1.0.0 */ ready: function () { app.setCountry(); app.bindCheckoutEvents(); app.checkCookies(); }, /** * Check if the current session has any relevant cookies set. */ checkCookies: function () { var visitor_id = app.getCookie( 'mi_drip_visitor_uuid' ); if ( visitor_id ) { app.visitor_uuid = visitor_id; } }, /** * Work with default events (blur, focus, etc). * * @since 1.0.0 */ bindCheckoutEvents: function () { if ( typeof _dcq === 'undefined' ) { return; } var body = $( 'body' ); var email_field = $( '#edd-email' ); // Tag upgrades. if ( body.hasClass( 'upgrade-purchase' ) && '' !== email_field.val() ) { _dcq.push( [ 'identify', { email: email_field.val(), abandoned_cart_url: app.edd.getAbandonmentURL( 'upgrade' ), tags: [ drip.tag_prefix + '-upgrade-to-' + drip.checkout_product_slug ] } ] ); } // Tag renewals. if ( $( '.edd-sl-renewal-details' ).length > 0 && '' !== email_field.val() ) { _dcq.push( [ 'identify', { email: email_field.val(), abandoned_cart_url: app.edd.getAbandonmentURL(), tags: [ drip.tag_prefix + '-renewal-intent', ] } ] ); } // Don't proceed for logged in users (upgrades). if ( body.hasClass( 'logged-in' ) ) { return; } // Send Email. email_field.on( 'blur', function () { var email = $( this ).val(), plan_id = app.edd.getPlanId(); if ( plan_id > 0 && __private.isEmailValid( email ) ) { var identity = { email: email, abandoned_cart_url: app.edd.getAbandonmentURL(), tags: [ drip.tag_prefix + '-abandoned', app.edd.getPlan() ], success: function ( response ) { if ( response.visitor_uuid ) { app.visitor_uuid = response.visitor_uuid; app.setCookie( 'mi_drip_visitor_uuid', app.visitor_uuid, 30 ); } } }; // Update the email instead of creating a new person for each request. if ( app.visitor_uuid ) { delete identity.email; identity.new_email = email; } if ( app.userCity ) { identity.city = app.userCity; } if ( app.userCountry ) { identity.country = app.userCountry; } _dcq.push( ['identify', identity] ); } } ); // Send User login. $( '#edd-user-login' ).on( 'blur', function () { var username = app.edd.getName( 'username' ), email = app.edd.getEmail(); if ( username.length && __private.isEmailValid( email ) ) { _dcq.push( [ 'identify', { email: email, username: username, abandoned_cart_url: app.edd.getAbandonmentURL() } ] ); } } ); // Send First name. $( '#edd-first' ).on( 'blur', function () { var first = app.edd.getName( 'first' ), email = app.edd.getEmail(); if ( first.length && __private.isEmailValid( email ) ) { _dcq.push( [ 'identify', { email: email, first_name: first, abandoned_cart_url: app.edd.getAbandonmentURL() } ] ); } } ); // Send Last name. $( '#edd-last' ).on( 'blur', function () { var last = app.edd.getName( 'last' ), email = app.edd.getEmail(); if ( last.length && __private.isEmailValid( email ) ) { _dcq.push( [ 'identify', { email: email, last_name: last, abandoned_cart_url: app.edd.getAbandonmentURL() } ] ); } } ); // Send phone $( '#edd-phone' ).on( 'blur', function () { var $phone = $( this ), fullNumber = $phone.intlTelInput( 'getNumber' ), email = app.edd.getEmail(); setTimeout( function() { if ( fullNumber.length && $phone.intlTelInput( 'isValidNumber' ) && __private.isEmailValid( email ) ) { _dcq.push( [ 'identify', { email: email, plusthis: fullNumber, tags: [ drip.tag_prefix + '-sms-cart-abandoned', ], abandoned_cart_url: app.edd.getAbandonmentURL() } ] ); } }, 50); } ); }, /** * Get the EDD specific data. * * @since 1.0.0 * * @type {Object} */ edd: { /** * Get the email from user registration form on Checkout page. * * @since 1.0.0 * * @return {string} */ getEmail: function () { var $email = $( '#edd-email' ); return $email.length ? $email.val() : ''; }, /** * Get the Plan from the cart on Checkout page. * * @since 1.0.0 * * @return {string} */ getPlan: function () { if ( drip.checkout_product_slug ) { return drip.tag_prefix + '-' + drip.checkout_product_slug; } return ''; }, /** * Get the Plan ID (donwload ID from EDD) from the cart on Checkout page. * * @since 1.0.0 * * @return {number} */ getPlanId: function () { var id = 0; if ( !$( '#edd_checkout_cart' ).length ) { return id; } var plan_id = $( '#edd_checkout_cart .edd_cart_item' ).first().data( 'download-id' ); if ( typeof plan_id !== 'undefined' ) { id = parseInt( plan_id, 10 ); } return id; }, /** * Get the applied discount from cart on Checkout page. * * @since 2.3.0 * * @return {string} */ getDiscount: function () { if ( !$( '.edd_discount_remove' ).length ) { return ''; } return $( '.edd_discount_remove' ).attr( 'data-code' ); }, /** * Get the user name from registration form on Checkout page. * * @since 1.0.0 * * @param {string} type Accepted values: 'username', 'first', 'last', 'full'. * * @return {string} */ getName: function ( type ) { var name = ''; if ( !$( '#edd_purchase_form' ).length ) { return name; } switch ( type ) { case 'username': var $username = $( '#edd-user-login' ); name = $username.length ? $username.val() : ''; break; case 'first': var $first = $( '#edd-first' ); name = $first.length ? $first.val() : ''; break; case 'last': var $last = $( '#edd-last' ); name = $last.length ? $last.val() : ''; break; case 'full': name = this.getName( 'first' ) + ' ' + this.getName( 'last' ); break; } return $.trim( name ); }, /** * Get the abandonment URL from the cart Checkout page. * * @since 2.3.0 * * @return {string} */ getAbandonmentURL: function ( type ) { var url = new URL( drip.checkout_url ); url.searchParams.set( 'nocache', 1 ); url.searchParams.set( 'edd_action', 'add_to_cart' ); url.searchParams.set( 'download_id', this.getPlanId() ); if ( 'upgrade' !== type ) { // For the upgrade abandonment we'll add the discount in DRIP. url.searchParams.set( 'discount', this.getDiscount() ); } url.searchParams.set( 'cemail', this.getEmail() ); url.searchParams.set( 'clogin', this.getEmail() ); url.searchParams.set( 'cfname', this.getName( 'first' ) ); url.searchParams.set( 'clname', this.getName( 'last' ) ); return url; } }, getCookie: function ( name ) { var v = document.cookie.match( '(^|;) ?' + name + '=([^;]*)(;|$)' ); return v ? v[2] : null; }, setCookie: function ( name, value, days ) { var expires = ''; if ( days ) { var date = new Date(); date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) ); expires = '; expires=' + date.toUTCString(); } document.cookie = name + '=' + ( value || '' ) + expires + '; path=/'; }, setCountry: function() { if ( Intl ) { var userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; var tzArr = userTimeZone.split("/"); if ( tzArr[ tzArr.length - 1 ] ) { app.userCity = tzArr[ tzArr.length - 1 ]; if ( timeZoneCityToCountry[ app.userCity ] ) { app.userCountry = timeZoneCityToCountry[ app.userCity ]; } } } } }; // Provide access to public functions/properties. return app; } )( document, window, jQuery ); // Initialize. Core.Drip.init();