You are here

google_adwords.module in Google AdWords Conversion Tracking 7.2

Same filename and directory in other branches
  1. 8 google_adwords.module
  2. 6 google_adwords.module
  3. 7 google_adwords.module

Google Adwords Conversion Tracking Module.

File

google_adwords.module
View source
<?php

/**
 * @file
 * Google Adwords Conversion Tracking Module.
 */

/**
 * Implements hook_field_info().
 *
 * Provides the description of the field.
 */
function google_adwords_field_info() {
  return array(
    // We name our field as the associative name of the array.
    'google_adwords_tracking' => array(
      'label' => t('Google Adwords'),
      'description' => t('Add the Google Adwords Trackin Conversion info for this entity.'),
      'settings' => array(),
      'instance_settings' => array(),
      'default_widget' => 'google_adwords_text',
      'default_formatter' => 'google_adwords_default',
    ),
  );
}

/**
 * Implements hook_field_is_empty().
 *
 * hook_field_is_emtpy() is where Drupal asks us if this field is empty.
 * Return TRUE if it does not contain data, FALSE if it does. This lets
 * the form API flag an error when required fields are empty.
 */
function google_adwords_field_is_empty($item, $field) {
  return empty($item['conversion_id']);
}

/**
 * Implements hook_field_formatter_info().
 *
 * @see google_adwords_field_formatter_view().
 */
function google_adwords_field_formatter_info() {
  return array(
    'google_adwords_default' => array(
      'label' => t('Default'),
      'field types' => array(
        'google_adwords_tracking',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 *
 * @see google_adwords_field_formatter_info().
 */
function google_adwords_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  global $user;
  $element = array();
  $trackable = 0;
  if (is_array($user->roles)) {
    foreach ($user->roles as $role) {
      $translated_role = t($role);
      $role = str_replace(' ', '_', $translated_role);
      $trackable += variable_get('google_adwords_track_' . $role, FALSE);
    }
  }
  if ($trackable > 0) {
    switch ($display['type']) {
      case 'google_adwords_default':
        foreach ($items as $delta => $item) {
          $element[$delta] = array(
            '#markup' => theme('google_adwords', $item),
            '#attached' => array(
              'css' => array(
                drupal_get_path('module', 'google_adwords') . '/google_adwords.css',
              ),
            ),
          );
        }
        break;
    }
  }
  return $element;
}

/**
 * Implements hook_field_widget_info().
 *
 * These widget types will eventually show up in hook_field_widget_form,
 * where we will have to flesh them out.
 *
 * @see google_adwords_field_widget_form()
 */
function google_adwords_field_widget_info() {
  return array(
    'google_adwords_text' => array(
      'label' => t('Google AdWords Text Field'),
      'field types' => array(
        'google_adwords_tracking',
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 *
 * hook_widget_form() is where Drupal tells us to create form elements for
 * our field's widget.
 */
function google_adwords_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  global $language;
  $widget = $instance['widget'];
  if ($widget['type'] == 'google_adwords_text') {
    $element['#type'] = 'fieldset';
    $element['#attributes']['class'][] = 'google-adwords-textfields';
    $element['#attached']['css'][] = drupal_get_path('module', 'google_adwords') . '/google_adwords.css';
    $element['conversion_id'] = array(
      '#type' => 'textfield',
      '#title' => t('Conversion ID'),
      '#default_value' => isset($items[$delta]['conversion_id']) ? $items[$delta]['conversion_id'] : variable_get('google_adwords_conversion_id', ''),
    );
    $element['conversion_language'] = array(
      '#type' => 'textfield',
      '#title' => t('Language'),
      '#default_value' => isset($items[$delta]['conversion_language']) ? $items[$delta]['conversion_language'] : variable_get('google_adwords_conversion_language', $language->language),
    );
    $element['conversion_format'] = array(
      '#type' => 'textfield',
      '#title' => t('Format'),
      '#default_value' => isset($items[$delta]['conversion_format']) ? $items[$delta]['conversion_format'] : variable_get('google_adwords_conversion_format', '2'),
    );
    $element['conversion_color'] = array(
      '#type' => 'textfield',
      '#title' => t('Color'),
      '#default_value' => isset($items[$delta]['conversion_color']) ? $items[$delta]['conversion_color'] : variable_get('google_adwords_conversion_color', 'FFFFFF'),
      '#size' => 7,
      '#maxlength' => 6,
    );
    $element['conversion_label'] = array(
      '#type' => 'textfield',
      '#title' => t('Report label'),
      '#default_value' => isset($items[$delta]['conversion_label']) ? $items[$delta]['conversion_label'] : '',
    );
  }
  return $element;
}

/**
 * Implements hook_theme().
 */
function google_adwords_theme() {
  return array(
    'google_adwords' => array(
      'variables' => array(
        'conversion_id' => NULL,
        'conversion_language' => NULL,
        'conversion_format' => NULL,
        'conversion_color' => NULL,
        'conversion_label' => NULL,
        'conversion_value' => NULL,
      ),
      'template' => 'google_adwords',
    ),
  );
}

/**
 * Implements template_preprocess_HOOK().
 * Loads the proper AdWords external JavaScript file.
 */
function template_preprocess_google_adwords(&$variables) {
  $variables['google_js'] = variable_get('google_adwords_external_script', 'https://www.googleadservices.com/pagead/conversion.js');
}

/**
 * Implements hook_permission().
 */
function google_adwords_permission() {
  return array(
    'administer google adwords module' => array(
      'title' => t('Administer Google Adwords Module'),
    ),
    'edit google adwords parameters' => array(
      'title' => t('Edit Google Adwords Parameters'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function google_adwords_menu() {
  $items = array();
  $items['admin/config/system/google_adwords'] = array(
    'title' => t('Google AdWords'),
    'description' => t('Google AdWords Settings page.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'google_adwords_admin_settings',
    ),
    'access arguments' => array(
      'administer google adwords module',
    ),
    'file' => 'google_adwords.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/system/google_adwords/default'] = array(
    'title' => t('Google Adwords'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
  );
  return $items;
}

/**
 * Register a new conversion tracking.
 *
 * The rules integration calls this function.
 * Use this to set a custom tracking not related to a entity.
 */
function google_adwords_add_tracking($conversion_id, $label = NULL, $value = NULL, $language = NULL, $color = NULL, $format = NULL, $currency = NULL) {
  $label = $label ? $label : base64_encode($conversion_id);
  $language = $language ? $language : 'en';
  $color = $color ? $color : 'FFFFFF';
  $format = $format ? $format : 1;
  $currency = $currency ? $currency : 'EUR';
  $_SESSION['google_adwords_rules_traking_codes'][$conversion_id] = array(
    'conversion_id' => $conversion_id,
    'conversion_label' => $label,
    'conversion_language' => $language,
    'conversion_color' => $color,
    'conversion_format' => $format,
    'conversion_value' => $value,
    'conversion_currency' => $currency,
  );
}

/**
 * Implements hook_page_build().
 *
 * This adds the tracking code into the page between the body tags.
 */
function google_adwords_page_build(&$page) {
  if (isset($_SESSION['google_adwords_rules_traking_codes'])) {
    $codes = $_SESSION['google_adwords_rules_traking_codes'];
    foreach ($codes as $code) {
      $markup = theme('google_adwords', $code);
      $page['page_bottom']['google_adwords_path'] = array(
        '#weight' => 55,
        '#markup' => $markup,
      );
    }
    unset($_SESSION['google_adwords_rules_traking_codes']);
  }
}