Last active
October 14, 2016 19:24
-
-
Save doublejosh/07a80da21cea899712824eb27c2e58d7 to your computer and use it in GitHub Desktop.
Campaign param parse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Determine relevant taxonomy terms from multi-code "utm_campaign" parameter. | |
* | |
* @example _tableau_landing_pages_parse_campaigns([type]-[topic]-[segment]-[audience]) | |
* | |
* @param string $param | |
* | |
* @return array | |
*/ | |
function _tableau_landing_pages_parse_campaigns($param) { | |
// Only compute once. | |
$result_found = &drupal_static(__FUNCTION__ . $param); | |
if ($result_found) { | |
return $result_found; | |
} | |
// Discover values, load mapping, and sanity check. | |
$campaignParts = explode('-', $param); | |
$mapping = _tableau_get_yaml('initiative_mapping', variable_get('tableau_landing_pages_initiative_mapping', '')); | |
if (!is_array($mapping)) { | |
return; | |
} | |
// Remove campaign type from raw param parsing. | |
if (count($campaignParts) === 4) { | |
array_shift($campaignParts); | |
} | |
// Hunt for mapped tid for each type of param facet. | |
$list = array(); | |
$mappers = array( | |
// Topics: maps to market buzz topic, org size, industry, capabilities, etc. | |
1 => 'topics', | |
// Segment: maps to department, industry, job role, org size, etc. | |
2 => 'segments', | |
// Audience: maps to job role, mostly. | |
3 => 'audience', | |
); | |
// YAML traversing. | |
foreach ($mappers as $param_num => $facet) { | |
// Sanity check for necessary data. | |
if (isset($campaignParts[$param_num]) && isset($mapping[$facet])) { | |
$param_val = $campaignParts[$param_num]; | |
// Find matching facet and add tid to list. | |
if (isset($mapping[$facet][$param_val])) { | |
$tid = $mapping[$facet][$param_val]; | |
$term = taxonomy_term_load($tid); | |
// Robust return data. | |
if (!empty($term) && is_object($term)) { | |
$vocabulary = taxonomy_vocabulary_load($term->vid); | |
$list[$vocabulary->machine_name][$tid] = $term->name; | |
} | |
} | |
} | |
} | |
return $list; | |
} | |
/** | |
* Implements hook_views_contextual_filter_function_info(). | |
* | |
* @return array | |
*/ | |
function tableau_landing_pages_views_contextual_filter_function_info() { | |
// Declare custom Views contextual filter function. | |
return (object) array( | |
'label' => t('Initiative params as term IDs'), | |
'function' => 'tableau_landing_pages_initiative_param_terms', | |
); | |
} | |
/** | |
* Make campaign URL params available as a tid list for Views. | |
* | |
* @return string | |
*/ | |
function tableau_landing_pages_initiative_param_terms() { | |
$params = drupal_get_query_parameters(); | |
if (!isset($params['utm_campaign'])) { | |
return FALSE; | |
} | |
$terms = _tableau_landing_pages_parse_campaigns($params['utm_campaign']); | |
return implode(',', _tableau_pluck($terms, 'tid')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment