View source
<?php
define('GOOGLE_ADWORDS_ADMIN_PERM', 'administer google adwords module');
define('GOOGLE_ADWORDS_EDIT_PERM', 'edit google adwords parameters');
function google_adwords_perm() {
return array(
GOOGLE_ADWORDS_ADMIN_PERM,
GOOGLE_ADWORDS_EDIT_PERM,
);
}
function google_adwords_menu() {
$items = array();
$items['admin/settings/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(
GOOGLE_ADWORDS_ADMIN_PERM,
),
'file' => 'google_adwords.admin.inc',
);
return $items;
}
function google_adwords_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
$form['google_adwords'] = array(
'#type' => 'fieldset',
'#access' => user_access(GOOGLE_ADWORDS_ADMIN_PERM),
'#title' => t('Google AdWords'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => FALSE,
);
$form['google_adwords']['google_adwords_activation'] = array(
'#type' => 'radios',
'#title' => t('Google AdWords parameters'),
'#description' => t('Provide Google AdWords for nodes of this content type.'),
'#default_value' => variable_get('google_adwords_activation_' . $form['#node_type']->type, 0),
'#options' => array(
t('Disabled'),
t('Enabled'),
),
);
}
if (isset($form['type']) && isset($form['#node'])) {
$node = $form['#node'];
if ($form['type']['#value'] . '_node_form' == $form_id && variable_get('google_adwords_activation_' . $node->type, FALSE)) {
$form['google_adwords'] = array(
'#type' => 'fieldset',
'#access' => user_access(GOOGLE_ADWORDS_EDIT_PERM),
'#title' => t('Google AdWords'),
'#collapsible' => TRUE,
'#collapsed' => empty($node->google_adwords),
'#prefix' => '<div class="google-adwords-params">',
'#suffix' => '</div>',
'#weight' => 30,
'#tree' => TRUE,
);
$form['google_adwords']['enabled'] = array(
'#type' => 'radios',
'#title' => t('Enable Google AdWords on this node'),
'#default_value' => isset($node->google_adwords['enabled']) ? $node->google_adwords['enabled'] : 0,
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$form['google_adwords']['label'] = array(
'#title' => t('Conversion Label'),
'#type' => 'textfield',
'#default_value' => isset($node->google_adwords['label']) ? $node->google_adwords['label'] : '',
);
$form['google_adwords']['format'] = array(
'#title' => t('Conversion Format'),
'#type' => 'select',
'#options' => _google_adwords_conversion_formats(),
'#default_value' => !empty($node->google_adwords['format']) ? $node->google_adwords['format'] : variable_get('google_adwords_conversion_format', '2'),
'#maxlength' => 10,
);
$form['google_adwords']['color'] = array(
'#type' => 'textfield',
'#title' => t('Conversion Color'),
'#default_value' => !empty($node->google_adwords['color']) ? $node->google_adwords['color'] : variable_get('google_adwords_conversion_color', 'FFFFFF'),
'#size' => 10,
'#maxlength' => 10,
);
}
}
}
function google_adwords_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
switch ($op) {
case 'load':
$adwords = google_adwords_load_params($node);
if (count($adwords) > 0) {
$fields['google_adwords'] = $adwords;
return $fields;
}
break;
case 'insert':
case 'update':
if (isset($node->google_adwords)) {
google_adwords_save_adwords($node);
}
break;
case 'delete':
google_adwords_delete_adwords($node);
break;
case 'delete revision':
google_adwords_delete_adwords_revision($node);
break;
}
}
function google_adwords_load_params($node) {
if ($node->vid) {
$result = db_fetch_array(db_query('SELECT * FROM {google_adwords} WHERE vid = %d', $node->vid));
$adwords = array(
'enabled' => $result['enabled'],
'label' => $result['label'],
'format' => $result['format'],
'color' => $result['color'],
);
return $adwords;
}
else {
return array();
}
}
function google_adwords_save_adwords($node) {
db_query('DELETE FROM {google_adwords} WHERE nid = %d AND vid = %d', $node->nid, $node->vid);
db_query("INSERT INTO {google_adwords} (nid, vid, enabled, label, format, color) VALUES (%d, %d, %d, '%s', '%s', '%s')", $node->nid, $node->vid, $node->google_adwords['enabled'], $node->google_adwords['label'], $node->google_adwords['format'], $node->google_adwords['color']);
}
function google_adwords_delete_adwords($node) {
db_query('DELETE FROM {google_adwords} WHERE nid = %d', $node->nid);
}
function google_adwords_delete_adwords_revision($node) {
db_query('DELETE FROM {google_adwords} WHERE nid = %d AND vid = %d', $node->nid, $node->vid);
}
function _google_adwords_view_adwords($node) {
global $user;
global $language;
$track = 0;
$page_match = 0;
$page_label = '';
if (is_array($user->roles)) {
foreach (array_keys($user->roles) as $rid) {
$track += variable_get("google_adwords_track_{$rid}", FALSE);
}
}
$path_vars = variable_get('google_adwords_conversion_pages', '');
if (!empty($path_vars)) {
$path = drupal_get_path_alias($_GET['q']);
$sets = explode("\n", $path_vars);
foreach ($sets as $k => $v) {
$set = explode('|', $v);
if (drupal_match_path($path, $set[1])) {
$page_match = 1;
$page_label = $set[0];
break;
}
}
}
if (arg(0) != 'admin' && $track > 0) {
if ($node->google_adwords['enabled'] != 0 || $page_match != 0) {
$google_adwords_label = !empty($page_label) ? $page_label : $node->google_adwords['label'];
$google_adwords_id = variable_get('google_adwords_conversion_id', 0);
$google_adwords_language = variable_get('google_adwords_conversion_language', $language->language);
$google_adwords_format = variable_get('google_adwords_conversion_format', '2');
$google_adwords_color = variable_get('google_adwords_conversion_color', 'FFFFFF');
$output = '';
$output .= "\n" . '<!-- Google Code for Conversion Page -->' . "\n";
$output .= '<script type="text/javascript">' . "\n";
$output .= '<!--' . "\n";
$output .= ' var google_conversion_id = ' . $google_adwords_id . ';' . "\n";
$output .= ' var google_conversion_language = "' . $google_adwords_language . '";' . "\n";
$output .= ' var google_conversion_format = "' . $google_adwords_format . '";' . "\n";
$output .= ' var google_conversion_color = "' . $google_adwords_color . '";' . "\n";
$output .= ' var google_conversion_label = "' . $google_adwords_label . '";' . "\n";
$output .= '//-->' . "\n";
$output .= '</script>' . "\n";
$output .= '<noscript>' . "\n";
$output .= ' <img height="1" width="1" border="0" src="https://www.googleadservices.com/pagead/conversion/' . $google_adwords_id . '/?label=' . $google_adwords_label . '&guid=ON&script=0" alt=""/>' . "\n";
$output .= '</noscript>' . "\n";
return $output;
}
}
}
function google_adwords_footer() {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
}
else {
$node = _google_adwords_empty_node();
}
$google_adwords_footer_script = _google_adwords_view_adwords($node);
if (!empty($google_adwords_footer_script)) {
$google_adwords_google_js = variable_get('google_adwords_external_script', 'https://www.googleadservices.com/pagead/conversion.js');
$google_adwords_footer_script .= '<script language="javaScript" type="text/javascript" src="' . $google_adwords_google_js . '"></script>' . "\n";
return $google_adwords_footer_script;
}
else {
return NULL;
}
}
function _google_adwords_conversion_formats() {
return array(
'1' => 'Single line',
'2' => 'Two lines',
'3' => 'Not displayed',
);
}
function _google_adwords_empty_node() {
$node = new stdClass();
$google_adwords = array(
'id' => '',
'label' => '',
'format' => '',
'color' => '',
'value' => '',
);
}