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

1 Answer 1


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

  1. Run composer require ramsey/uuid in the root directory of your project

  2. 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;
  1. Add the following in the Module's init() method, right after the line that says // Custom initialization code goes here...:
    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
  1. Replace the REPLACE-THIS strings with your own Tracking ID and Event options

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.