PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.4.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.4.3
3.4.3 3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 All 196 releases
convertkit / resources / frontend / js / convertkit.js

convertkit.js in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.4.3, at resources/frontend/js/convertkit.js

301 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Tags the given subscriber ID with the given tag
12 *
13 * @since 1.9.6
14 *
15 * @param int subscriber_id Subscriber ID
16 * @param string tag Tag
17 * @param int post_id WordPress Post ID
18 */
19 function convertKitTagSubscriber( subscriber_id, tag, post_id ) {
20
21 if ( convertkit.debug ) {
22 console.log( 'convertKitTagSubscriber' );
23 console.log( convertkit );
24 console.log( subscriber_id );
25 console.log( tag );
26 console.log( post_id );
27 }
28
29 fetch(
30 convertkit.ajaxurl,
31 {
32 method: 'POST',
33 headers: {
34 'Content-Type': 'application/x-www-form-urlencoded',
35 },
36 body: new URLSearchParams(
37 {
38 action: 'convertkit_tag_subscriber',
39 convertkit_nonce: convertkit.nonce,
40 subscriber_id: subscriber_id,
41 tag: tag,
42 post_id: post_id,
43 }
44 )
45 }
46 )
47 .then(
48 function ( response ) {
49 if ( convertkit.debug ) {
50 console.log( response );
51 }
52
53 return response.json();
54 }
55 )
56 .then(
57 function ( result ) {
58 if ( convertkit.debug ) {
59 console.log( result );
60 }
61
62 convertKitRemoveSubscriberIDFromURL( window.location.href );
63 }
64 )
65 .catch(
66 function ( error ) {
67 if ( convertkit.debug ) {
68 console.error( error );
69 }
70
71 convertKitRemoveSubscriberIDFromURL( window.location.href );
72 }
73 );
74
75 }
76
77 /**
78 * Gets the subscriber ID for the given email address, storing
79 * it in the `ck_subscriber_id` cookie if it exists.
80 *
81 * Typically called when the user completes a ConvertKit Form
82 * that has either "Auto-confirm new subscribers" or
83 * "Send subscriber to thank you page" enabled (both scenarios
84 * include a ck_subscriber_id).
85 *
86 * @since 1.9.6
87 *
88 * @param int id Subscriber ID
89 */
90 function convertStoreSubscriberIDInCookie( subscriber_id ) {
91
92 if ( convertkit.debug ) {
93 console.log( 'convertStoreSubscriberIDInCookie' );
94 console.log( subscriber_id );
95 }
96
97 fetch(
98 convertkit.ajaxurl,
99 {
100 method: 'POST',
101 headers: {
102 'Content-Type': 'application/x-www-form-urlencoded',
103 },
104 body: new URLSearchParams(
105 {
106 action: 'convertkit_store_subscriber_id_in_cookie',
107 convertkit_nonce: convertkit.nonce,
108 subscriber_id: subscriber_id
109 }
110 )
111 }
112 )
113 .then(
114 function ( response ) {
115 if ( convertkit.debug ) {
116 console.log( response );
117 }
118
119 return response.json();
120 }
121 )
122 .then(
123 function ( result ) {
124 if ( convertkit.debug ) {
125 console.log( result );
126 }
127
128 convertKitRemoveSubscriberIDFromURL( window.location.href );
129 }
130 )
131 .catch(
132 function ( error ) {
133 if ( convertkit.debug ) {
134 console.error( error );
135 }
136
137 convertKitRemoveSubscriberIDFromURL( window.location.href );
138 }
139 );
140
141 }
142
143 /**
144 * Gets the subscriber ID for the given email address, storing
145 * it in the `ck_subscriber_id` cookie if it exists.
146 *
147 * Typically called when the user completes a ConvertKit Form
148 * that has either "Auto-confirm new subscribers" or
149 * "Send subscriber to thank you page" enabled (both scenarios
150 * include a ck_subscriber_id).
151 *
152 * @since 1.9.6
153 *
154 * @param string emailAddress Email Address
155 */
156 function convertStoreSubscriberEmailAsIDInCookie( emailAddress ) {
157
158 if ( convertkit.debug ) {
159 console.log( 'convertStoreSubscriberEmailAsIDInCookie' );
160 console.log( emailAddress );
161 }
162
163 fetch(
164 convertkit.ajaxurl,
165 {
166 method: 'POST',
167 headers: {
168 'Content-Type': 'application/x-www-form-urlencoded',
169 },
170 body: new URLSearchParams(
171 {
172 action: 'convertkit_store_subscriber_email_as_id_in_cookie',
173 convertkit_nonce: convertkit.nonce,
174 email: emailAddress
175 }
176 )
177 }
178 )
179 .then(
180 function ( response ) {
181 if ( convertkit.debug ) {
182 console.log( response );
183 }
184 }
185 )
186 .then(
187 function ( result ) {
188 if ( convertkit.debug ) {
189 console.log( result );
190 }
191 }
192 )
193 .catch(
194 function ( error ) {
195 if ( convertkit.debug ) {
196 console.error( error );
197 }
198 }
199 );
200
201 }
202
203 /**
204 * Remove the url subscriber_id url param
205 *
206 * The 'ck_subscriber_id' should only be set on URLs included on
207 * links from a ConvertKit email with no other URL parameters.
208 * This function removes the parameters so a customer won't share
209 * a URL with their subscriber ID in it.
210 *
211 * @param url
212 */
213 function convertKitRemoveSubscriberIDFromURL( url ) {
214
215 var clean_url = url.substring( 0, url.indexOf( "?ck_subscriber_id" ) );
216 var title = document.getElementsByTagName( "title" )[0].innerHTML;
217 if ( clean_url ) {
218 window.history.pushState( null, title, clean_url );
219 }
220
221 }
222
223 /**
224 * Utility function to pause for the given number of milliseconds
225 *
226 * @since 1.9.6
227 *
228 * @param int milliseconds
229 */
230 function convertKitSleep( milliseconds ) {
231
232 var start = new Date().getTime();
233 for (var i = 0; i < 1e7; i++) {
234 if ((new Date().getTime() - start) > milliseconds) {
235 break;
236 }
237 }
238
239 }
240
241 /**
242 * Register events
243 */
244 document.addEventListener(
245 'DOMContentLoaded',
246 function () {
247
248 if ( convertkit.subscriber_id > 0 && convertkit.tag && convertkit.post_id ) {
249 // If the user can be detected as a ConvertKit Subscriber (i.e. their Subscriber ID is in a cookie or the URL),
250 // and the Page/Post they are viewing has a Tag specified, subscribe them to the tag.
251 convertKitTagSubscriber( convertkit.subscriber_id, convertkit.tag, convertkit.post_id );
252 } else if ( convertkit.subscriber_id > 0 ) {
253 // If the user can be detected as a ConvertKit Subscriber (i.e. their Subscriber ID is in a cookie or the URL),
254 // update the cookie now.
255 convertStoreSubscriberIDInCookie( convertkit.subscriber_id );
256 }
257
258 // Store subscriber ID as a cookie from the email address used when a ConvertKit Form is submitted.
259 document.addEventListener(
260 'click',
261 function (e) {
262 // Check if the form submit button was clicked, or the span element was clicked and its parent is the form submit button.
263 if ( ! e.target.matches( '.formkit-submit' ) && ! e.target.parentElement.matches( '.formkit-submit' ) ) {
264 if ( convertkit.debug ) {
265 console.log( 'not a ck form' );
266 }
267
268 return;
269 }
270
271 // Get email address.
272 let emailAddress = document.querySelector( 'input[name="email_address"]' ).value;
273
274 // If the email address is empty, don't attempt to get the subscriber ID by email.
275 if ( ! emailAddress.length ) {
276 if ( convertkit.debug ) {
277 console.log( 'email empty' );
278 }
279
280 return;
281 }
282
283 // If the email address is invalid, don't attempt to get the subscriber ID by email.
284 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,}))$/;
285 if ( ! validator.test( emailAddress.toLowerCase() ) ) {
286 if ( convertkit.debug ) {
287 console.log( 'email not an email address' );
288 }
289
290 return;
291 }
292
293 // Wait a moment before sending the AJAX request.
294 convertKitSleep( 2000 );
295 convertStoreSubscriberEmailAsIDInCookie( emailAddress );
296 }
297 );
298
299 }
300 );
301