DiscipleTools / disciple-tools-webform

Add a lead form to any website and integrate those leads into the Disciple.Tools system. Build custom lead forms through an admin interface, assign leads to dispatcher, and tag with sources. Special security design allows for forms to be served from one system and privately transferred to the Disciple.Tools system.
https://disciple.tools/plugins/webform/
Other
2 stars 6 forks source link

Contact Source. How to pass source or tag variable in URL? #52

Open Strellman opened 2 years ago

Strellman commented 2 years ago

It would be really helpful to know how somebody got to our website! We are storing a cookie to indicate which ad, or which person led the contact to us. Now I want to pass that field to DT in the URL so that as the contact submits their form, the dispatcher will know where they came from (possibly a city, a team, or a person). If someone can at least point me to the section of code, or the variables to set, that would even help. I would like to create a snippet to store this info.

Another approach would be to do the form on the host website and just send the fields to DT somehow.

Strellman commented 2 years ago

WPFORMS has done a lot of this. See https://wpforms.com/wpforms-hidden-field/ Is there anyway to to pass the WPFORM data to DT?

corsacca commented 2 years ago

It would be really helpful to know how somebody got to our website!

Agreed. The current webform does not really support a way of doing that. One solution could be to add a hook to https://github.com/DiscipleTools/disciple-tools-webform/blob/e38458afa94819c97e2a4c4091ef228d153218f1/includes/create-contact.php#L75 and hook into it with some php code from your server.

Or, you could write your own form and send the fields to D.T. This is possible via the API and the Site to Site like authentication

The webform could support hidden fields. Since we display the form via iframe, updating those fields from the website is challenging. Next upgrade

Strellman commented 2 years ago

Thanks Corsacca, that is very helpful. I really don't understand how to add a hook there without forking and maintaining that code, which is beyond me. Is there anyway to add a filter that pulls variables from the url?

Cookies can't be pulled from a different site into DT, but maybe they could be passed as parameters.  What about getting the cookie before generating the iframe and passing it along with referer in the iframe url? Tell me if it makes sense to you. 

This snippet is a shortcode to insert our cookies as tags into the iframe url before it is generated. Usage: [dt_source][/dt_source]

//register shortcode
add_shortcode('dt_source','dt_source');

function dt_source($attr, $iframestring = null){
//send DT tag of referer at least
    $tags = 'tags[]='.$_SERVER['HTTP_REFERER'];
//check to see if user has our initial url cookie, send it
    $contact_url = $_COOKIE['contact_url'];
    if (isset($contact_url)) $tags .= ('&tags[]='.$contact_url);
//if user has source cookie send it
    $contact_source = $_COOKIE['contact_source'];
    if (isset($contact_source)) $tags.= ('&tags[]='.$contact_source);
//insert tags before token parameter in $iframestring 
    $tags .= "&token=";
$iframestring = str_replace("token=", $tags, $iframestring);
return $iframestring;
} //end function

//Then on the DT instance use this snippet of code (How do I hook to it?)
//Get tags into DT contact from iframe url
add_filter( "dt_custom_post_create_fields", "dt_post_create_fields", 1, 2 );
function dt_post_create_fields( $fields, $post_type){
//check if we are dealing with a contact
if ($post_type === "contacts"){
    //check if the tags field is already set
    if ( !isset( $fields["tags"] )){
        $tags = sanitize_text_field($_GET['tags'] ); 
            if (isset($tags))  {
            //define the tags field
            $fields["tags"] = $tags;
            }
        }
    }
return $fields;
}
corsacca commented 2 years ago

That could possibly work Step 0 use the campaigns field or create the needed fields in D.T Step 1 pass the parameters to the iframe Step 2 read the url parameters via javascript and submit the form with those parameters. Step 3 Make sure those parameters are passed along in the form_submit() function

The campaigns field (acts like the tags field) might be the right field for this data. And could be supported out of the box. If the data is different for each contact, then a text field would be better. And step 3 would be more complicated

We'd want to use a field that already exists in D.T or set up a custom field, so we won't need to do anything in dt_post_create_fields. and instead make sure form_submit() in the webform plugin gets and sends the right data