First, I had to create the new save task for Webforms to access in Sitecore. There are already a bunch of these that come shipped with WFFM. They’re tasks like Save to Database, Send Mail, Subscribe to Target Audience, or it can be tied to the OMS by registering a campaign or completing a goal. After traversing to the save tasks for Webforms (Content Editor -> System -> Modules -> Web Forms For Marketers -> Settings -> Actions -> Save Actions) I just had to do a quick right-click -> Insert -> Save Action. Voila, I have my new save action. But now, I need to give it functionality!
For this part, I had to go over to my project in Visual Studio. Web Forms is set up that you can define the C# class you want tied to the new save action, but it does have to abide by a few rules. First, it needs to extend the Sitecore.Form.Core.Client.Data.Submit.ISubmit class. This library is added to your project files when you install the WFFM module. Before you can start accessing it, don’t forget to reference the .dll file in your project. Since I’m extending this class, I need to override the Execute function for that class, which is where I’m going to stick the code to run on the submit of our web form.
public class GoogleAdWordsScript : ISubmit
public virtual void Submit(ID formid, AdaptedResultList fields)
Once I had this class defined and compiled, I needed to tie it to my Save Action in Sitecore. Back at my custom save action in Sitecore, there are two fields that need to be filled: Assembly and Class. The assembly is the namespace in which I defined my new C# class in, and the class is the name of the class. You’ll need the namespace and the classname in the Class field for this to work correctly. Once I had these filled out, I can add this action to my web form submit actions, but it’s not going to do anything for me just yet. But now that I have the building blocks laid for my new functionality, I can actually write the functionality!
Page page = HttpContext.Current.Handler as Page;
string googleAdWordsArguments = @"var google_conversion_id = 1234567890;
var google_conversion_language = ""en_US"";
var google_conversion_format = ""1"";
var google_conversion_color = ""666666"";
var google_conversion_label = ""Purchase"";";
page.ClientScript.RegisterStartupScript(this.GetType(), "myID", googleAdWordsArguments, true);
The first parameter, this.GetType(), gives the Type of the current instance. This includes information about the page object, including the namespace, name, assembly, etc. So, if you have any questions about the Assembly and Class fields on the Sitecore object, this is where you find your answer.
The next parameter, “myID”, is the key of the script I’m adding. This doesn’t show on the front-end, but the ScriptManager uses it for organizing multiple scripts. Each script you add needs to have a unique key, otherwise the ScriptManager sees them as duplicates.
But again, I have to insert this line only after the web form is submitted using C#. Simple – RegisterClientScriptInclude will do that for me.
The two arguments are the key for the client script (which needs to be unique like we talked about earlier!), and the source of the script I want referenced. That’s done.
I’ll make the
string insertScript = "";
insertScript += "var noscript = document.createElement('noscript');";
Next, I need to add the img object to the
insertScript += "noscript.innerHTML = """";
Now that I have the
insertScript += "document.getElementsByTagName('body').appendChild(noscript);";
page.ClientScript.RegisterStartupScript(this.GetType(), "myID3", insertScript, true);