You are here

function simplenews_statistics_mail_alter in Simplenews Statistics 7

Same name and namespace in other branches
  1. 6.3 simplenews_statistics.module \simplenews_statistics_mail_alter()
  2. 6 simplenews_statistics.module \simplenews_statistics_mail_alter()
  3. 6.2 simplenews_statistics.module \simplenews_statistics_mail_alter()
  4. 7.2 simplenews_statistics.module \simplenews_statistics_mail_alter()

Implements hook_mail_alter().

Parses all the links in the email so they can be tracked. Also adds a hidden image to the body to track opens.

File

./simplenews_statistics.module, line 186
Main simplenews statistics file.

Code

function simplenews_statistics_mail_alter(&$message) {
  if ($message['id'] == 'simplenews_node' || $message['id'] == 'simplenews_test') {
    $node = $message['params']['simplenews_source']
      ->getNode();
    $subscriber = $message['params']['simplenews_source']
      ->getSubscriber();

    // During testing the snid might be unset. Use 0 in that case. This will
    // make sure that the link will still work but won't be counted.
    $snid = isset($subscriber->snid) ? $subscriber->snid : 0;

    // Optionally ignore $snid tracking for test sends.
    $track_test = variable_get('simplenews_statistics_track_test', 0);
    if ($track_test == 0 && $message['id'] == 'simplenews_test') {
      $snid = 0;
    }

    // Parse links in body.
    _simplenews_statistics_parse_links($message['body'], $node->nid, $snid);

    // Add view image.
    _simplenews_statistics_image_tag($message['body'], $node->nid, $snid);
  }

  // If this is a true newsletter send then we also want to update the
  // newsletter record {simplenews_statistics} table.
  if ($message['id'] == 'simplenews_node') {
    $subscriber_count = simplenews_statistics_count_subscribers($node->nid);

    // Set the send_start_timestamp if this is the first newsletter.
    db_update('simplenews_statistics')
      ->fields(array(
      'send_start_timestamp' => time(),
      'subscriber_count' => $subscriber_count,
    ))
      ->condition('nid', $node->nid)
      ->condition('send_start_timestamp', 0)
      ->execute();

    // Set send_end_timestamp to time() for every newsletter sent.
    db_update('simplenews_statistics')
      ->fields(array(
      'send_end_timestamp' => time(),
    ))
      ->condition('nid', $node->nid)
      ->execute();
  }
}