# fluentform/1.2.4/app/Modules/Form/Analytics.php

Fluent Forms – Customizable Contact Forms, Survey, Quiz, &amp; Conversational Form Builder, version 1.2.4. 120 lines.

- Page: https://pluginprobe.com/plugins/fluentform/1.2.4/code/app/Modules/Form/Analytics.php
- Raw: https://pluginprobe.com/plugins/fluentform/1.2.4/raw/app/Modules/Form/Analytics.php
- Modified: 2018-01-04T13:54:28+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/fluentform/1.2.4/code/app/Modules/Form/Analytics.php#L10-L20`.

```php
<?php

namespace FluentForm\App\Modules\Form;

use FluentForm\App\Services\Browser\Browser;
use FluentForm\Framework\Foundation\Application;

class Analytics
{
	/**
     * @var \FluentForm\Framework\Foundation\Application
     */
    protected $app;

    /**
     * Build the instance
     *
     * @param \FluentForm\Framework\Foundation\Application $app
     */
    public function __construct(Application $app)
    {
        $this->app = $app;
    }

	/**
	 * Save form view analytics data
	 * 
	 * @return json respoonse
	 */
	public function record()
	{
		return $this->saveViewAnalytics();
	}

	/**
     * Save form view analytics data
     * @return void
     */
    private function saveViewAnalytics()
    {
        $formId = $this->app->request->get('formId');
        if (!$form = wpFluent()->table('fluentform_forms')->find($formId)) {
            return;
        }

        $userId = null;
        if ($user = wp_get_current_user()) {
            $userId = $user->ID;
        }

        $browser = new Browser;

        $data = array(
            'count' => 1,
            'form_id' => $formId,
            'user_id' => $userId,
            'ip' => $this->app->request->getIp(),
            'browser' => $browser->getBrowser(),
            'platform' => $browser->getPlatform(),
            'created_at' => date('Y-m-d H:i:s'),
            'source_url' => $this->app->request->server('HTTP_REFERER'),
        );

        $query = wpFluent()
        ->table('fluentform_form_analytics')
        ->where('ip', $data['ip'])
        ->where('form_id', $data['form_id'])
        ->where('source_url', $data['source_url']);

        try {
            if (($record = $query->first())) {
                $query->update(array('count' => ++$record->count));
            } else {
                wpFluent()->table('fluentform_form_analytics')->insert($data);
                $this->increaseTotalViews($formId);
            }

            wp_send_json_success(array(
                'status' => true,
            ), 200);

        } catch(\Exception $e) {
            wp_send_json_success(array(
                'status' => false,
            ), 200);
        }
    }

    /**
     * Store (create/update) total view of a form
     * @param  int $formId
     * @return void
     */
    private function increaseTotalViews($formId)
    {
        // check if meta is already exists or not
        $hasCount = wpFluent()
                    ->table('fluentform_form_meta')
                    ->where('meta_key', '_total_views')
                    ->where('form_id', $formId)
                    -> first();

        if($hasCount) {
            wpFluent()
                ->table('fluentform_form_meta')
                ->where('id', $hasCount->id)
                ->update(array(
                    'value' => intval($hasCount->value) + 1
                ));
        } else {
            wpFluent()
                ->table('fluentform_form_meta')
                ->insert(array(
                    'value' => 1,
                    'form_id' => $formId,
                    'meta_key' => '_total_views'
                ));
        }
    }
}
```
