google_adwords_remarketing.module in Google Adwords Remarketing 7
Same filename and directory in other branches
Drupal Module: Google Adwords Remarketing Adds the required Javascript to the bottom of speficied Drupal pages to allow the feature provided by the Google Adwords Remarketing package.
@author Chris Calip <http://drupal.org/user/210499>
File
google_adwords_remarketing.moduleView source
<?php
/**
* @file
* Drupal Module: Google Adwords Remarketing
* Adds the required Javascript to the bottom of speficied Drupal pages
* to allow the feature provided by the Google Adwords Remarketing package.
*
* @author Chris Calip <http://drupal.org/user/210499>
*/
/**
* Default external js to be used.
*/
define('GARC_DEFAULT_EXT_JS', 'http://www.googleadservices.com/pagead/conversion.js');
/**
* Implements hook_menu().
*/
function google_adwords_remarketing_menu() {
$items['admin/config/services/garc'] = array(
'title' => 'Google Adwords Remarketing',
'description' => "Configure what Google Adwords Remarketing Campaign gets attached in your site's closure region.",
'page callback' => 'google_adwords_remarketing_admin_display',
'access arguments' => array(
'administer garc',
),
'file' => 'google_adwords_remarketing.admin.inc',
);
$items['admin/config/services/garc/%/configure'] = array(
'title' => 'Configure campaign',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'google_adwords_remarketing_admin_configure',
4,
),
'access arguments' => array(
'administer garc',
),
'type' => MENU_CALLBACK,
'file' => 'google_adwords_remarketing.admin.inc',
);
$items['admin/config/services/garc/%/delete'] = array(
'title' => 'Delete campaign',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'google_adwords_remarketing_campaign_delete',
4,
),
'access arguments' => array(
'administer garc',
),
'type' => MENU_CALLBACK,
'file' => 'google_adwords_remarketing.admin.inc',
);
$items['admin/config/services/garc/add'] = array(
'title' => 'Add campaign',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'google_adwords_remarketing_add_campaign_form',
),
'access arguments' => array(
'administer garc',
),
'type' => MENU_LOCAL_ACTION,
'file' => 'google_adwords_remarketing.admin.inc',
);
return $items;
}
/**
* Implements hook_help().
*/
function google_adwords_remarketing_help($path, $arg) {
switch ($path) {
case 'admin/config/services/garc':
$output = t('<a href="@garc_url">Google Adwords Remarketing</a> is a feature of interest-based advertising available in the Audiences tab. Remarketing allows you to reach people who previously visited your website, and match the right people with the right message. You can show users these messages as they browse sites across the Google Display Network.', array(
'@garc_url' => 'http://adwords.google.com/support/aw/bin/answer.py?hl=en&answer=173945',
));
$output .= '<p>' . t('Click the <em>configure</em> link next to each Campaign to configure its specific title and visibility settings.') . '</p>';
return $output;
}
}
/**
* Implements hook_perm().
*/
function google_adwords_remarketing_permission() {
return array(
'administer garc' => array(
'title' => 'administer google adwords remarketing',
'description' => 'use PHP for google adwords remarketing campaign GARC visibility',
),
);
}
/**
* Implements hook_theme().
*/
function google_adwords_remarketing_theme() {
return array(
'google_adwords_remarketing_admin_display_form' => array(
'render element' => 'form',
),
);
}
/**
* Returns information from database about a user-created GARC
* google adwords remarketing campaign.
*
* @param $garcid
* ID of the garc to get information for.
* @return
* Associative array of information stored in the database for this garc.
* Array keys:
* - garcid: GARC ID.
* - many more , see google_adwords_remarketing schema
* @ google_adwords_remarketing.install
*/
function google_adwords_remarketing_campaign_get($garcid) {
return db_query("SELECT * FROM {google_adwords_remarketing} WHERE garcid = :garcid", array(
':garcid' => $garcid,
))
->fetchObject();
}
/**
* Implements hook_page_alter().
*/
function google_adwords_remarketing_page_alter(&$page) {
global $theme;
$custom_theme = variable_get('theme_default', 'garland');
// @TODO multi themes currently only supports the default theme.
// Don't load javascript on page that use different theme than default.
// Fixed issue https://drupal.org/node/2274137
if ($theme != $custom_theme) {
return;
}
$assigned_garc = _google_adwords_remarketing_get_assigned_campaign();
if ($assigned_garc == FALSE) {
return;
}
$page['page_bottom']['garc'] = array(
'#type' => 'markup',
'#markup' => _google_adwords_remarketing_campaign_to_script($assigned_garc),
);
}
/**
* Utility function to get the assigned garc for the current page.
* @return array $assigned_garc or false if no record found
*/
function _google_adwords_remarketing_get_assigned_campaign() {
global $user, $theme;
$garc_list =& drupal_static(__FUNCTION__);
if (!isset($garc_list)) {
if ($cache = cache_get('google_adwords_remarketing_garc_list')) {
$garc_list = $cache->data;
}
else {
// Do the logic which one to used.
$query = db_select('google_adwords_remarketing', 'g');
$query
->leftjoin('google_adwords_remarketing_roles', 'r', 'g.garcid = r.garcid');
$garc_list = $query
->fields('g')
->fields('r', array(
'rid',
))
->condition('g.status', 1)
->orderBy('g.weight')
->distinct()
->execute()
->fetchAll();
cache_set('google_adwords_remarketing_garc_list', $garc_list, 'cache');
}
}
$garcs = array();
foreach ($garc_list as $garc) {
$role_match = FALSE;
if ($garc->rid) {
if (in_array($garc->rid, array_keys($user->roles))) {
$role_match = TRUE;
}
}
else {
$role_match = TRUE;
}
$theme_match = FALSE;
if ($garc->theme) {
if ($garc->theme == $theme) {
$theme_match = TRUE;
}
}
else {
$theme_match = TRUE;
}
// Match path if necessary.
$page_match = FALSE;
if ($garc->pages) {
if ($garc->visibility < 2) {
$path = drupal_get_path_alias($_GET['q']);
// Compare with the internal and path alias (if any).
$page_match = drupal_match_path($path, $garc->pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $garc->pages);
}
// When $block->visibility has a value of 0, the block is displayed on
// all pages except those listed in $block->pages. When set to 1, it
// is displayed only on those pages listed in $block->pages.
$page_match = !($garc->visibility xor $page_match);
}
else {
$page_match = php_eval($garc->pages);
}
}
else {
$page_match = TRUE;
}
if ($theme_match && $role_match && $page_match) {
$garcs[$garc->weight][$garc->garcid] = $garc;
}
}
if (empty($garcs)) {
return FALSE;
}
// get the highest priority garc; first by weight and then on id
$garcs_shifted = array_shift($garcs);
// Fixed issue https://drupal.org/node/2150629
$assigned_garc = array_shift($garcs_shifted);
// Return the assigned garc for this page.
return $assigned_garc;
}
/**
* Utility function to convert a garc record to an output script.
* @param object $garc
* @return string $garc_output_script
*/
function _google_adwords_remarketing_campaign_to_script($garc) {
$output = '';
$output .= "\n" . '<!-- Google Adwords Conversion Code generated by drupal google_adwords_remarketing, campaign name: ' . $garc->info . ' -->' . "\n";
$output .= '<script type="text/javascript">' . "\n";
$output .= '/* <![CDATA[ */' . "\n";
$output .= ' var google_conversion_id = ' . $garc->conversion_id . ';' . "\n";
$output .= ' var google_conversion_language = "' . $garc->language . '";' . "\n";
$output .= ' var google_conversion_format = "' . $garc->format . '";' . "\n";
$output .= ' var google_conversion_color = "' . $garc->color . '";' . "\n";
if (!empty($garc->label)) {
$output .= ' var google_conversion_label = "' . $garc->label . '";' . "\n";
}
$output .= ' var google_custom_params = ' . $garc->params . ';' . "\n";
$output .= ' var google_remarketing_only = ' . $garc->remarketing . ';' . "\n";
$output .= '/* ]]> */' . "\n";
$output .= '</script>' . "\n";
$output .= '<script language="javaScript" type="text/javascript" src="' . $garc->ext_js . '"></script>' . "\n";
$output .= '<noscript>' . "\n";
$output .= ' <img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/' . $garc->conversion_id;
if (!empty($garc->label)) {
$output .= '/?value=0&label=' . $garc->label . '&guid=ON&script=0"/>' . "\n";
}
else {
$output .= '/?value=0&guid=ON&script=0"/>' . "\n";
}
$output .= '</noscript>' . "\n";
$garc_output_script = $output;
return $garc_output_script;
}
Functions
Name![]() |
Description |
---|---|
google_adwords_remarketing_campaign_get | Returns information from database about a user-created GARC google adwords remarketing campaign. |
google_adwords_remarketing_help | Implements hook_help(). |
google_adwords_remarketing_menu | Implements hook_menu(). |
google_adwords_remarketing_page_alter | Implements hook_page_alter(). |
google_adwords_remarketing_permission | Implements hook_perm(). |
google_adwords_remarketing_theme | Implements hook_theme(). |
_google_adwords_remarketing_campaign_to_script | Utility function to convert a garc record to an output script. |
_google_adwords_remarketing_get_assigned_campaign | Utility function to get the assigned garc for the current page. |
Constants
Name![]() |
Description |
---|---|
GARC_DEFAULT_EXT_JS | Default external js to be used. |