Commit ecab6ce9 authored by Julian Pustkuchen's avatar Julian Pustkuchen
Browse files

Issue #3244408 by stevetweeddale, Anybody: Add AJAX command

parent 0bc4b0b5
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
facebook_pixel:
  version: 1.0
  version: 2.0
  header: true
  js:
    js/facebook_pixel.js: {}
@@ -7,3 +7,4 @@ facebook_pixel:
    - core/jquery
    - core/drupal
    - core/drupalSettings
    - core/drupal.ajax
+16 −0
Original line number Diff line number Diff line
/**
 * @file
 * facebook_pixel.js
 *
 * Defines the behavior of the facebook pixel module.
 */

(function (Drupal) {

  'use strict';

  Drupal.AjaxCommands.prototype.facebook_pixel_track = function (ajax, response, status) {
    fbq('track', response.event, response.data);
  }

})(Drupal);
+53 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\facebook_pixel\Ajax;

use Drupal\Core\Ajax\CommandInterface;

/**
 * Logs an event to facebook pixel.
 *
 * This command is implemented in Drupal.AjaxCommands.prototype.facebook_pixel_
 * track.
 */
class FacebookPixelTrackCommand implements CommandInterface {

  /**
   * Event name.
   *
   * @var string
   */
  protected $event;

  /**
   * Event data.
   *
   * @var mixed
   */
  protected $data;

  /**
   * Constructs a \Drupal\facebook_pixel\Ajax\FacebookPixelTrackCommand object.
   *
   * @param string $event
   *   The event name.
   * @param mixed $data
   *   Assigned value.
   */
  public function __construct($event, $data) {
    $this->event = $event;
    $this->data = $data;
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    return [
      'command' => 'facebook_pixel_track',
      'event' => $this->event,
      'data' => $this->data,
    ];
  }

}