console.log('INTEGRATION HOOKS'); var integrations_array = [], functions_array_index = 0, integrations_array_index = 0, testing = false; var zapier_webhook = 'https://hooks.zapier.com/hooks/catch/5882429/4hq5pn0/'; integrations_array.push({ 'event_hooks':{ 'complete_campaign':{ 'functions':[ { 'name':zapier_post_webhook, 'parameters': ['complete_campaign'] } ] }, } }); console.log( integrations_array ); // zapier Functions function zapier_post_webhook( arr ){ var user_obj = iah_return_user_obj(), zapier_obj = { "email": user_obj.email, "trigger": arr[0], "integration": "za", "dataFields": {} }, callback_func = arr[1]; var url = zapier_webhook if (enabled_integrations){ if (enabled_integrations.indexOf('zapier') == -1) { callback_func(); return; } } if ("undefined" !== typeof zapier_webhook_override){ url = zapier_webhook_override } zapier_obj.dataFields['user'] = user_obj; zapier_obj.dataFields['campaign'] = iah_get_campaign_info(); if ( user_obj['cong_info'] ) zapier_obj.dataFields['cong'] = user_obj['cong_info']; if ( getUrlVars() ) zapier_obj.dataFields['meta'] = getUrlVars(); console.log( 'zapier_track_events object', zapier_obj ); if ( testing ){ setTimeout(function(){ console.log( 'zapier_track_events complete' ); callback_func(); },1000); }else{ $.post( url, JSON.stringify(zapier_obj), function(data){ console.log( data ); callback_func(); }); } } // End zapier Functions window.iah_integrations_hooks = function( event ){ return new Promise( function(resolve, reject) { if ( integrations_array.length === 0 ){ resolve() return; } var current_integration = integrations_array[integrations_array_index], current_integration_function_obj_array = ( current_integration['event_hooks'][event] ) ? current_integration['event_hooks'][event]['functions'] : [], current_function_obj = ( current_integration_function_obj_array[functions_array_index] ) ? current_integration_function_obj_array[functions_array_index] : null, current_function = ( current_function_obj ) ? current_function_obj['name'] : null, current_function_callback = function(){ // if the function you're on is the last one in the array of functions for the current integration... if ( functions_array_index === current_integration_function_obj_array.length ){ // ... and there are no more integrations left, invoke the original callback and exit this function if ( integrations_array_index === (integrations_array.length - 1) ){ resolve() return; } // otherwise move into the next integration and reset the function counter to zero integrations_array_index ++; functions_array_index = 0; }else{ // if there are functions left in this array, iterate to the next one functions_array_index ++; } resolve(iah_integrations_hooks( event )); }, current_function_parameters = ( current_function_obj && current_function_obj['parameters'] ) ? current_function_obj['parameters'] : []; // if there's a function to be called, push the callback function to the array of parameters if ( current_function ){ current_function_parameters.push( current_function_callback ); current_function( current_function_parameters ); }else{ // otherwise just call the callback function current_function_callback(); } }) }