1/** 2 * Frontend functionality for subscribers and tags. 3 * 4 * @since 1.9.6 5 * 6 * @package ConvertKit 7 * @author ConvertKit 8 */ 9 10/** 11 * Gets the subscriber ID for the given email address, storing 12 * it in the `ck_subscriber_id` cookie if it exists. 13 * 14 * Typically called when the user completes a ConvertKit Form 15 * that has either "Auto-confirm new subscribers" or 16 * "Send subscriber to thank you page" enabled (both scenarios 17 * include a ck_subscriber_id). 18 * 19 * @since 1.9.6 20 * 21 * @param int id Subscriber ID 22 */ 23function convertStoreSubscriberIDInCookie( subscriber_id ) { 24 25 if ( convertkit.debug ) { 26 console.log( 'convertStoreSubscriberIDInCookie' ); 27 console.log( subscriber_id ); 28 } 29 30 fetch( 31 convertkit.ajaxurl, 32 { 33 method: 'POST', 34 headers: { 35 'Content-Type': 'application/x-www-form-urlencoded', 36 }, 37 body: new URLSearchParams( 38 { 39 action: 'convertkit_store_subscriber_id_in_cookie', 40 convertkit_nonce: convertkit.nonce, 41 subscriber_id: subscriber_id 42 } 43 ) 44 } 45 ) 46 .then( 47 function ( response ) { 48 if ( convertkit.debug ) { 49 console.log( response ); 50 } 51 52 return response.json(); 53 } 54 ) 55 .then( 56 function ( result ) { 57 if ( convertkit.debug ) { 58 console.log( result ); 59 } 60 61 convertKitRemoveSubscriberIDFromURL( window.location.href ); 62 } 63 ) 64 .catch( 65 function ( error ) { 66 if ( convertkit.debug ) { 67 console.error( error ); 68 } 69 70 convertKitRemoveSubscriberIDFromURL( window.location.href ); 71 } 72 ); 73 74} 75 76/** 77 * Gets the subscriber ID for the given email address, storing 78 * it in the `ck_subscriber_id` cookie if it exists. 79 * 80 * Typically called when the user completes a ConvertKit Form 81 * that has either "Auto-confirm new subscribers" or 82 * "Send subscriber to thank you page" enabled (both scenarios 83 * include a ck_subscriber_id). 84 * 85 * @since 1.9.6 86 * 87 * @param string emailAddress Email Address 88 */ 89function convertStoreSubscriberEmailAsIDInCookie( emailAddress ) { 90 91 if ( convertkit.debug ) { 92 console.log( 'convertStoreSubscriberEmailAsIDInCookie' ); 93 console.log( emailAddress ); 94 } 95 96 fetch( 97 convertkit.ajaxurl, 98 { 99 method: 'POST', 100 headers: { 101 'Content-Type': 'application/x-www-form-urlencoded', 102 }, 103 body: new URLSearchParams( 104 { 105 action: 'convertkit_store_subscriber_email_as_id_in_cookie', 106 convertkit_nonce: convertkit.nonce, 107 email: emailAddress 108 } 109 ) 110 } 111 ) 112 .then( 113 function ( response ) { 114 if ( convertkit.debug ) { 115 console.log( response ); 116 } 117 } 118 ) 119 .then( 120 function ( result ) { 121 if ( convertkit.debug ) { 122 console.log( result ); 123 } 124 } 125 ) 126 .catch( 127 function ( error ) { 128 if ( convertkit.debug ) { 129 console.error( error ); 130 } 131 } 132 ); 133 134} 135 136/** 137 * Remove the url subscriber_id url param 138 * 139 * The 'ck_subscriber_id' should only be set on URLs included on 140 * links from a ConvertKit email with no other URL parameters. 141 * This function removes the parameters so a customer won't share 142 * a URL with their subscriber ID in it. 143 * 144 * @param url 145 */ 146function convertKitRemoveSubscriberIDFromURL( url ) { 147 148 var clean_url = url.substring( 0, url.indexOf( "?ck_subscriber_id" ) ); 149 var title = document.getElementsByTagName( "title" )[0].innerHTML; 150 if ( clean_url ) { 151 window.history.pushState( null, title, clean_url ); 152 } 153 154} 155 156/** 157 * Utility function to pause for the given number of milliseconds 158 * 159 * @since 1.9.6 160 * 161 * @param int milliseconds 162 */ 163function convertKitSleep( milliseconds ) { 164 165 var start = new Date().getTime(); 166 for (var i = 0; i < 1e7; i++) { 167 if ((new Date().getTime() - start) > milliseconds) { 168 break; 169 } 170 } 171 172} 173 174/** 175 * Register events 176 */ 177document.addEventListener( 178 'DOMContentLoaded', 179 function () { 180 181 if ( convertkit.subscriber_id > 0 ) { 182 // If the user can be detected as a ConvertKit Subscriber (i.e. their Subscriber ID is in a cookie or the URL), 183 // update the cookie now. 184 convertStoreSubscriberIDInCookie( convertkit.subscriber_id ); 185 } 186 187 // Store subscriber ID as a cookie from the email address used when a ConvertKit Form is submitted. 188 document.addEventListener( 189 'click', 190 function (e) { 191 // Check if the form submit button was clicked, or the span element was clicked and its parent is the form submit button. 192 if ( ! e.target.matches( '.formkit-submit' ) && ! e.target.parentElement.matches( '.formkit-submit' ) ) { 193 if ( convertkit.debug ) { 194 console.log( 'not a ck form' ); 195 } 196 197 return; 198 } 199 200 // Get email address. 201 let emailAddress = document.querySelector( 'input[name="email_address"]' ).value; 202 203 // If the email address is empty, don't attempt to get the subscriber ID by email. 204 if ( ! emailAddress.length ) { 205 if ( convertkit.debug ) { 206 console.log( 'email empty' ); 207 } 208 209 return; 210 } 211
212 // If the email address is invalid, don't attempt to get the subscriber ID by email. 213 var validator = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 214 if ( ! validator.test( emailAddress.toLowerCase() ) ) { 215 if ( convertkit.debug ) { 216 console.log( 'email not an email address' ); 217 } 218 219 return; 220 } 221 222 // Wait a moment before sending the AJAX request. 223 convertKitSleep( 2000 ); 224 convertStoreSubscriberEmailAsIDInCookie( emailAddress ); 225 } 226 ); 227 228 } 229);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.