<p>One way you could do this is on the backend via a module and the FreeForm events:</p>

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

<pre><code>use yii\base\Event;

use GuzzleHttp\Client;
use Ramsey\Uuid\Uuid;
use Solspace\Freeform\Services\FormsService;
use Solspace\Freeform\Events\Forms\AfterSubmitEvent;
</code></pre>

<ol start="3">
<li>Add the following in the Module's <code>init()</code> method, right after the line that says <code>// Custom initialization code goes here...</code>:</li>
</ol>

<pre><code>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
            ]
        ]);
    }
);
</code></pre>

<ol start="4">
<li><p>Replace the <code>REPLACE-THIS</code> strings with your own Tracking ID and Event options</p></li>
<li><p>Uncomment the following line in <code>config/app.php</code>:</p></li>
</ol>

<pre><code>//'bootstrap' =&gt; ['my-module'],
</code></pre>

<p><em>Tip: You have the form and the submission available to you as <code>$form</code> and <code>$submission</code> 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).</em></p>

<hr>

<p>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.)</p>

CC BY-SA 4.0
2019-11-06T19:08:53.713Z
2019-11-06T19:08:53.713Z
Jalen Davenport
<p>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.</p>

<p>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.</p>

<p>How do I detect if any form was submitted?
Freeform's docs explicitly name a form handle, which isn't a dynamic approach ( <a href="https://docs.solspace.com/craft/freeform/v3/template-functions/freeform.submissions.html#usage-in-templates" rel="nofollow noreferrer">https://docs.solspace.com/craft/freeform/v3/template-functions/freeform.submissions.html#usage-in-templates</a> ).</p>

<plugin-freeform><freeform>
Freeform: How do I detect if a form was submitted?
# Freeform: How do I detect if a form was submitted?

(Asked by: username_0 on 2019-11-06)

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> ).

**Tags:** plugin-freeform, freeform

---

## 1 answers

### Answer by username_1 on 2019-11-06. Score: 2 | Accepted answer

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->getForm();
        $submission = $event->getSubmission();

        $uuid4 = Uuid::uuid4();

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


---

