开发者

Merging array values depending on what values are set

开发者 https://www.devze.com 2023-04-11 13:07 出处:网络
Currently, I am trying to make my URL shor开发者_开发百科tener script able to track UTM variables from Google Analytics. I also want to provide my own (campaign/keyword/source/etc...) variables.

Currently, I am trying to make my URL shor开发者_开发百科tener script able to track UTM variables from Google Analytics. I also want to provide my own (campaign/keyword/source/etc...) variables.

I am trying to sift through $_GET, pick out "approved" key/value pairs and set each key with the condition that UTM variables take higher precedence (I only want to store one value in the database).

My code is currently:

//Parse $_GET and only get the key/pairs that we need and store them in global $url_params for use through out the script
function set_url_params($allowed=NULL) { // $allowed is arg to add new values in future dev 
global $url_params;

$allowed = array( 
    'l', // Redirect key (e.g http://example.com?l=a1dFd7)
    'utm_campaign', 
    'campaign', 
    'utm_source' , 
    'source', 
    'utm_medium', 
    'medium', 
    'utm_term', 
    'term', 
    'keyword',
    'kw',
    );

    $approved = array_intersect_key($_GET, array_flip($allowed));

    foreach($approved as $key => $value) {
        strip_tags(urldecode(trim($value)));
        $url_params[$key] = $value;
    }

    //Assign variables to global $url_params variable so other functions can use them.
    //NOTE: Google Analytics UTM parameters take precedence over script values.
    $url_params['l'] = isset($approved['l']) ? $approved['l'] : NULL ;
    $url_params['campaign'] = isset($approved['utm_campaign']) ? $approved['utm_campaign'] : $approved['campaign'];
    $url_params['source'] = isset($approved['utm_source']) ? $approved['utm_source'] : $approved['source'];
    $url_params['medium'] = isset($approved['utm_medium']) ? $approved['utm_medium'] : $approved['medium'];
    $url_params['term'] = isset($approved['utm_term']) ? $approved['utm_term'] : $approved['term'];
    $url_params['keyword'] = isset($approved['keyword']) ? $approved['keyword'] : $approved['kw'];
    // Just in case $url_params doesn't have a 'keyword' set, we will use 'term' instead.
    $url_params['keyword'] = isset($url_params['keyword']) ? $url_params['keyword'] : $url_params['term'];

}

I basically want to find a cleaner way of doing this without all the isset()'s. I also get NOTICE errors (running in E_ALL) for undefined variables that I would like to unset so I don't get those errors.


Probably something along the lines of this?

$params = array(
    'l'        => array('default' => null),
    'campaign' => array('default' => null, 'alt' => 'utm_campaign'),
    ...
);

$url_params = array();

foreach ($params as $key => $info) {
    $url_params[$key] = $info['default'];
    if (isset($info['alt'], $_GET[$info['alt']])) {
        $url_params[$key] = $_GET[$info['alt']];
    }
    if (isset($_GET[$key]) {
        $url_params[$key] = $_GET[$key];
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号