You are here

google_adwords_signup.module in Google AdWords Conversion Tracking 8

Same filename and directory in other branches
  1. 7.2 modules/google_adwords_signup/google_adwords_signup.module

Google Adwords User Conversion Tracking SubModule.

File

modules/google_adwords_signup/google_adwords_signup.module
View source
<?php

/**
 * @file
 * Google Adwords User Conversion Tracking SubModule.
 */

/**
 * Implements hook_form_alter().
 */
function google_adwords_signup_form_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id) {

  // Add to Google AdWords form so we can track user registration.
  if ($form_id == 'google_adwords_admin_settings') {
    $form['user_conversion'] = array(
      '#type' => 'fieldset',
      '#title' => t('User Conversion settings'),
      '#collapsible' => TRUE,
    );
    $form['user_conversion']['google_adwords_user_conversion_id'] = array(
      '#type' => 'textfield',
      '#title' => t('Conversion ID'),
      '#default_value' => \Drupal::config('google_adwords_signup.settings')
        ->get('google_adwords_user_conversion_id'),
      '#size' => 15,
      '#maxlength' => 255,
      '#required' => TRUE,
      '#description' => '',
    );
    $form['user_conversion']['google_adwords_user_conversion_language'] = array(
      '#type' => 'textfield',
      '#title' => t('Conversion Language'),
      '#default_value' => \Drupal::config('google_adwords_signup.settings')
        ->get('google_adwords_user_conversion_language'),
      '#size' => 15,
      '#maxlength' => 255,
      '#required' => TRUE,
      '#description' => '',
    );
    $form['user_conversion']['google_adwords_user_conversion_format'] = array(
      '#type' => 'textfield',
      '#title' => t('Conversion Format'),
      '#default_value' => \Drupal::config('google_adwords_signup.settings')
        ->get('google_adwords_user_conversion_format'),
      '#size' => 15,
      '#maxlength' => 255,
      '#required' => TRUE,
      '#description' => '',
    );
    $form['user_conversion']['google_adwords_user_conversion_color'] = array(
      '#type' => 'textfield',
      '#title' => t('Conversion Color'),
      '#default_value' => \Drupal::config('google_adwords_signup.settings')
        ->get('google_adwords_user_conversion_color'),
      '#size' => 15,
      '#maxlength' => 255,
      '#required' => TRUE,
      '#description' => '',
    );
  }
  if ($form_id == 'user_register_form') {

    // Need to add the ability to shut this off with an admin setting.
    $form['#submit'][] = 'google_adwords_signup_conversion';
  }
}

/**
 * Custom submit handler for the user registration form for conversion tracking.
 */
function google_adwords_signup_conversion($form, &$form_state) {
  $_SESSION['google_adwords_signup_conversion'] = TRUE;
}

/**
 * Implements hook_preprocess_page().
 */
function google_adwords_signup_preprocess_page($variables) {
  if (isset($_SESSION['google_adwords_signup_conversion'])) {
    $language = \Drupal::languageManager()
      ->getCurrentLanguage();
    unset($_SESSION['google_adwords_signup_conversion']);
    $conversion_values = array(
      'conversion_id' => \Drupal::config('google_adwords_signup.settings')
        ->get('google_adwords_user_conversion_id'),
      'conversion_langauge' => \Drupal::config('google_adwords_signup.settings')
        ->get('google_adwords_user_conversion_language'),
      'conversion_format' => \Drupal::config('google_adwords_signup.settings')
        ->get('google_adwords_user_conversion_format'),
      'conversion_color' => \Drupal::config('google_adwords_signup.settings')
        ->get('google_adwords_user_conversion_color'),
      'conversion_label' => 'User Signup',
    );
    drupal_add_js('\\/* <![CDATA[ *\\/
        var google_conversion_id = ' . $conversion_values['conversion_id'] . ';
        var google_conversion_language = "' . $conversion_values['conversion_langauge'] . '";
        var google_conversion_format = "' . $conversion_values['conversion_format'] . '";
        var google_conversion_color = "' . $conversion_values['conversion_color'] . '";
        var google_conversion_label = "' . $conversion_values['conversion_label'] . '";
     \\/* ]]> *\\/', array(
      'type' => 'inline',
      'scope' => 'footer',
      'weight' => 5,
    ));
    drupal_add_js(\Drupal::config('google_adwords_signup.settings')
      ->get('google_adwords_external_script'), array(
      'type' => 'external',
      'scope' => 'footer',
      'weight' => 6,
    ));
  }
}

Functions

Namesort descending Description
google_adwords_signup_conversion Custom submit handler for the user registration form for conversion tracking.
google_adwords_signup_form_alter Implements hook_form_alter().
google_adwords_signup_preprocess_page Implements hook_preprocess_page().