Freeform: How do I detect if a form was submitted? I'm need to send a google analytics tracking event when a form is submitted. To do that, I need to know on any page of the site if a user has landed there as a result of a form being submitted.

Given that the client can create their own forms, I can't rely on URL parameters, and it could be any form, so I can't set the form handle manually.

How do I detect if any form was submitted?
Freeform's docs explicitly name a form handle, which isn't a dynamic approach ( https://docs.solspace.com/craft/freeform/v3/template-functions/freeform.submissions.html#usage-in-templates ).
One way you could do this is on the backend via a module and the FreeForm events:


Run composer require ramsey/uuid in the root directory of your project
Add the following lines to the top of modules/Module.php right after the line that says use Craft;


use yii\base\Event;

use GuzzleHttp\Client;
use Ramsey\Uuid\Uuid;
use Solspace\Freeform\Services\FormsService;
use Solspace\Freeform\Events\Forms\AfterSubmitEvent;



Add the following in the Module's init() method, right after the line that says // Custom initialization code goes here...:


Event::on(
    FormsService::class,
    FormsService::EVENT_AFTER_SUBMIT,
    static function(AfterSubmitEvent $event) {
        $form = $event-&gt;getForm();
        $submission = $event-&gt;getSubmission();

        $uuid4 = Uuid::uuid4();

        $client = new Client();
        $client-&gt;post('https://www.google-analytics.com/collect', [
            'headers' =&gt; [
                'User-Agent' =&gt; Craft::$app-&gt;request-&gt;getUserAgent(),
            ],
            'query' =&gt; [
                'v' =&gt; 1,
                'tid' =&gt; 'REPLACE-THIS',      // Tracking ID from GA
                'cid' =&gt; $uuid4-&gt;toString(),
                't'   =&gt; 'event',
                'ec'  =&gt; 'REPLACE-THIS',      // Event Category
                'ea'  =&gt; 'REPLACE-THIS',      // Event Action
                'el'  =&gt; 'REPLACE-THIS',      // Event Label
            ]
        ]);
    }
);



Replace the REPLACE-THIS strings with your own Tracking ID and Event options
Uncomment the following line in config/app.php:


//'bootstrap' =&gt; ['my-module'],


Tip: You have the form and the submission available to you as $form and $submission respectively in case you want to use any of their values to populate your Event options (i.e. use the form name as the Event Label or something).



Hopefully that helps! (Disclaimer: I just threw this together so it's not really "tested"  - if you do run into issues ping me on Discord and I'll help you get it working.)