Skip to content

Commit

Permalink
Setup browser caching, added referral parameter to the script_src,
Browse files Browse the repository at this point in the history
to prevent over-caching.

Added customer_session_init event tracking, which required building a
separate Observer, common code between the observers was moved into a trait.
  • Loading branch information
digitaladapt committed Aug 10, 2020
1 parent 81b8181 commit 605549e
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 19 deletions.
1 change: 1 addition & 0 deletions Controller/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function execute(): ResultInterface

return $this->rawFactory->create()
->setHttpResponseCode(200)
->setHeader('Cache-Control', 'private, max-age=15') /* browser may cache for 15 seconds */
->setHeader('Content-Type', 'application/javascript')
->setContents(<<<JS
(function () {
Expand Down
48 changes: 48 additions & 0 deletions Observer/CustomerTracking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace VigilantForm\MagentoKit\Observer;

use Magento\Customer\Model\Session;
use Magento\Framework\Event\{Observer, ObserverInterface};
use Magento\Framework\Filesystem\DirectoryList;
use Psr\Log\LoggerInterface;
use VigilantForm\MagentoKit\Traits\TrackPage;
use VigilantForm\MagentoKit\VigilantFormMagentoKit;

class CustomerTracking implements ObserverInterface
{
use TrackPage;

/** @var DirectoryList */
protected $directory;

/** @var LoggerInterface */
protected $logger;

/** @var VigilantFormMagentoKit */
protected $vfmk;

/**
* @param DirectoryList $directory
* @param LoggerInterface $logger
*/
public function __construct(DirectoryList $directory, LoggerInterface $logger)
{
$this->directory = $directory;
$this->logger = $logger;
$this->vfmk = null;
}

/**
* @param Observer $observer
*/
public function execute(Observer $observer)
{
/* this allows us to work with the freshly created customer session */
$session = $observer->getData('customer_session');
if ($session instanceof Session) {
$this->vfmk = new VigilantFormMagentoKit($this->directory, $session, $this->logger);
$this->trackSource();
}
}
}
11 changes: 4 additions & 7 deletions Observer/PageTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace VigilantForm\MagentoKit\Observer;

use Magento\Framework\Event\{Observer, ObserverInterface};
use VigilantForm\MagentoKit\Traits\TrackPage;
use VigilantForm\MagentoKit\VigilantFormMagentoKit;

class PageTracking implements ObserverInterface
{
use TrackPage;

/** @var VigilantFormMagentoKit */
protected $vfmk;

Expand All @@ -23,12 +26,6 @@ public function __construct(VigilantFormMagentoKit $vfmk)
*/
public function execute(Observer $observer)
{
/* get the file extension of the uri, will be blank for extensionless filenames, such as directories */
$extension = pathinfo(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), PATHINFO_EXTENSION);

/* if extension contains "htm" or blank string (directory) */
if (stripos($extension, 'htm') !== false || $extension === '') {
$this->vfmk->trackSource();
}
$this->trackSource();
}
}
4 changes: 2 additions & 2 deletions SessionAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public function __construct(SessionManagerInterface $session)

public function exists(string $key): bool
{
$getKey = "get{$key}";
return !!$this->session->$getKey();
$hasKey = "has{$key}";
return $this->session->$hasKey();
}

public function get(string $key, $default = null)
Expand Down
27 changes: 27 additions & 0 deletions Traits/TrackPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace VigilantForm\MagentoKit\Traits;

use VigilantForm\MagentoKit\VigilantFormMagentoKit;

trait TrackPage
{
/** @var VigilantFormMagentoKit */
protected $vfmk;

protected function trackSource(): void
{
/* get the file extension of the uri, will be blank for extensionless filenames, such as directories */
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$extension = pathinfo($path, PATHINFO_EXTENSION);

/* if extension contains "htm" or blank string (directory) */
if (stripos($extension, 'htm') !== false || $extension === '') {
/* track page, if request is expected referral, flag it as such */
$this->vfmk->trackSource(
strpos($path, 'vigilant_form/index/index') !== false ||
strpos($path, 'customer/section/load') !== false
);
}
}
}
21 changes: 12 additions & 9 deletions VigilantFormMagentoKit.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,35 +61,38 @@ public function trackSource(bool $useReferral = false): void
}

/**
* Call once per html form, reusing the html multiple times will cause problems.
* If user has javascript disabled, to pass the honeypot, they'll be asked
* a simple math problem. If they have javascript, they will see nothing.
* Reusing the html multiple times is allowed, but only on the same page.
* If user has javascript disabled, they will failed the honeypot.
* Regardless of if they have javascript, they will see nothing.
* @see VigilantFormKit::generateHoneypot()
* @return string Returns chunk of html to insert into a form.
*/
public function generateHoneypot(): string
{
$this->trackSource();
/* referral is only used to prevent over-caching */
$refPath = htmlentities(urlencode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
$data = (object)$this->getInstance()->getStatus(false);
return <<<HTML
<div class="{$data->script_class}"></div>
<script src="{$data->script_src}"></script>
<script src="{$data->script_src}?referral={$refPath}"></script>
HTML;
}

/**
* @see VigilantFormKit::submitForm()
* @param array $fields The user submission, such as $_POST.
* @param string $website Optional, name of the website that the form exists on.
* @param string $form_title Optional, name of the form was submitted.
* @param array|null $fields Optional, the user submission, defaults to $_POST.
* @param string|null $form_title Optional, name of the form was submitted, default from config.
* @param string|null $website Optional, name of the website that the form exists on, default from config.
* @return bool Returns true on success, will throw an exception otherwise.
* @throws UnexpectedValueException when attempt to store form is unsuccessful.
*/
public function submitForm(array $fields, string $website = null, string $form_title = null): bool
public function submitForm(array $fields = null, string $form_title = null, string $website = null): bool
{
$this->trackSource();
$website = $website ?? $this->getWebsite();
$fields = $fields ?? $_POST;
$form_title = $form_title ?? $this->getFormTitle();
$website = $website ?? $this->getWebsite();
return $this->getInstance()->submitForm($website, $form_title, $fields);
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"require": {
"php": ">=7.1.0",
"digitaladapt/vigilant-form-kit": "^1.3.0-beta",
"digitaladapt/vigilant-form-kit": "^1.3.1",
"ext-json": "*"
},
"autoload": {
Expand Down
3 changes: 3 additions & 0 deletions etc/frontend/events.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="customer_session_init">
<observer name="VigilantFormCustomerTracking" instance="VigilantForm\MagentoKit\Observer\CustomerTracking" disabled="false" />
</event>
<event name="controller_front_send_response_before">
<observer name="VigilantFormPageTracking" instance="VigilantForm\MagentoKit\Observer\PageTracking" disabled="false" />
</event>
Expand Down

0 comments on commit 605549e

Please sign in to comment.