class EventStorageService in Commerce Google Tag Manager 8
Same name and namespace in other branches
- 8.2 src/EventStorageService.php \Drupal\commerce_google_tag_manager\EventStorageService
Stores all tracked Enhanced Ecommerce events in a private tempstore.
Hierarchy
- class \Drupal\commerce_google_tag_manager\EventStorageService
Expanded class hierarchy of EventStorageService
3 files declare their use of EventStorageService
- EventsController.php in src/
Controller/ EventsController.php - EventStorageServiceTest.php in tests/
src/ Unit/ EventStorageServiceTest.php - EventTrackerServiceTest.php in tests/
src/ Unit/ EventTrackerServiceTest.php
1 string reference to 'EventStorageService'
1 service uses EventStorageService
File
- src/
EventStorageService.php, line 13
Namespace
Drupal\commerce_google_tag_managerView source
class EventStorageService {
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
private $eventDispatcher;
/**
* The tempstore object.
*
* @var \Drupal\Core\TempStore\PrivateTempStore
*/
private $tempStore;
/**
* Constructs the EventTrackerService service.
*
* @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
* The tempstore factory.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
*/
public function __construct(PrivateTempStoreFactory $temp_store_factory, EventDispatcherInterface $event_dispatcher) {
$this->tempStore = $temp_store_factory
->get('commerce_google_tag_manager');
$this->eventDispatcher = $event_dispatcher;
}
/**
* Get all tracked Enhanced Ecommerce events.
*
* Note: If you send the events to Google Tag Manager, make
* sure to flush event data afterwards to prevent double tracking.
*
* @return array
* All tracked Enhanced Ecommerce events.
*/
public function getEvents() {
$events = $this->tempStore
->get('events') ?: [];
return array_values($events);
}
/**
* Add event data to the storage.
*
* Computes a hash from the given event data to prevent storing
* the exact same event multiple times.
*
* @param array $event_data
* The event data to store.
*
* @return $this
* Return the EventStorageService object.
*/
public function addEvent(array $event_data) {
$events = (array) $this->tempStore
->get('events') ?: [];
$hash = $this
->hash($event_data);
if (!isset($events[$hash])) {
$event = new AlterEventDataEvent($event_data);
$this->eventDispatcher
->dispatch(EnhancedEcommerceEvents::ALTER_EVENT_DATA, $event);
$events[$this
->hash($event_data)] = $event
->getEventData();
$this->tempStore
->set('events', $events);
}
return $this;
}
/**
* Delete all stored event data.
*
* @return bool
* The flush results.
*/
public function flush() {
return $this->tempStore
->delete('events');
}
/**
* Compute a hash from the given event data.
*
* @param array $event_data
* The event data to store.
*
* @return string
* The hash value.
*/
private function hash(array $event_data) {
return md5(json_encode($event_data));
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EventStorageService:: |
private | property | The event dispatcher. | |
EventStorageService:: |
private | property | The tempstore object. | |
EventStorageService:: |
public | function | Add event data to the storage. | |
EventStorageService:: |
public | function | Delete all stored event data. | |
EventStorageService:: |
public | function | Get all tracked Enhanced Ecommerce events. | |
EventStorageService:: |
private | function | Compute a hash from the given event data. | |
EventStorageService:: |
public | function | Constructs the EventTrackerService service. |