Add support for the following events:

  • ViewContent (node, taxonomy term and commerce product pages)
  • InitiateCheckout
  • Purchase
  • CompleteRegistration
  • AddToCart
  • AddToWishlist (Commerce Wishlist/Flag implementation)

Or would you rather have this as a separate module?

Comments

introfini created an issue. See original summary.

rob230’s picture

I don't currently have time to do this, but if I ever add it to a site with Drupal Commerce then it may get done. I'm thinking we can have a submodule that depends on the commerce module.

geza84’s picture

I do not understand. The module sends these events right?
According to the description: "You can implement hook_meta_conversions_api_event_names() to add your own events. This is only necessary if you intend on making it possible to disable the event."
I see that this module only sends a PageView event. That's right?

rob230’s picture

You can make it send any event using hooks.

rob230’s picture

I won't be adding Commerce events because I think they are too custom and people will basically always want to set up the event themselves and pass whatever data it is they need.

As a quick example, you can use an EventSubscriber to trigger when an order is placed:


namespace Drupal\example_module\EventSubscriber;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\commerce_order\Entity\OrderItemInterface;
use Drupal\meta_conversions_api\Services\MetaClient;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Event subscriber for Meta Conversions API.
 */
class MetaConversionsEventSubscriber implements EventSubscriberInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Meta client.
   *
   * @var \Drupal\meta_conversions_api\Services\MetaClient
   */
  protected MetaClient $metaClient;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\meta_conversions_api\Services\MetaClient $meta_client
   *   Meta client.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, MetaClient $meta_client) {
    $this->entityTypeManager = $entity_type_manager;
    $this->metaClient = $meta_client;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    return [
      'commerce_order.place.post_transition' => 'orderPlaced',
    ];
  }

  /**
   * Respond to an order being placed.
   *
   * Sends event to Meta Conversions API.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The event.
   */
  public function orderPlaced(WorkflowTransitionEvent $event) {
    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $event->getEntity();

    $billingProfile = $order->getBillingProfile();
    /** @var \Drupal\address\Plugin\Field\FieldType\AddressItem $address */
    $address = $billingProfile->get('address')->get(0);

    $orderItems = $order->getItems();
    $orderItem = reset($orderItems);
    if (!$orderItem instanceof OrderItemInterface) {
      // This shouldn't ever happen.
      return;
    }

    $productVariation = $orderItem->getPurchasedEntity();
    $price = $order->getTotalPrice();

    $userData = [
      'first_name' => $address->getGivenName(),
      'last_name' => $address->getFamilyName(),
      'email' => $order->getEmail(),
      'city' => $address->getLocality(),
      'country_code' => $address->getCountryCode(),
      'zip_code' => $address->getPostalCode(),
    ];

    $customData = [
      'order_id' => $order->id(),
      'currency' => $price->getCurrencyCode(),
      'value' => number_format($price->getNumber(), 2, '.', ''),
      'custom_properties' => [
        'order_type' => $orderItem->bundle(),
        'product_variation' => $productVariation->bundle(),
      ],
    ];

    if ($orderItem->bundle() == 'something') {
      $customData['custom_properties']['custom_field'] = $order->get('field_custom_field')->value;
    }

    $this->metaClient->sendRequest([
      'event_name' => 'Purchase',
      'event_id' => 'purchase-' . $order->id(),
    ], $userData, $customData);
  }

}

Also need to add the event:

/**
 * Implements hook_meta_conversions_api_event_names().
 */
function example_module_meta_conversions_meta_conversions_api_event_names(): array {
  return [
    'Purchase',
  ];
}
rob230’s picture

Status: Active » Closed (won't fix)

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.