Last active
October 9, 2018 21:11
-
-
Save Zodiac1978/8fbf5d67063cb213f530812d0f5b247d to your computer and use it in GitHub Desktop.
!!! WORK IN PROGRESS !!! – Adds a new column in the statify table for the User Agent.
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 | |
/** | |
* Plugin Name: Statify UA Tracking | |
* Description: Adds a new column in the statify table for the User Agent string. | |
* Plugin URI: http://torstenlandsiedel.de | |
* Version: 1.0 | |
* Author: Torsten Landsiedel | |
* Author URI: http://torstenlandsiedel.de | |
* Licence: GPL 2 | |
* License URI: http://opensource.org/licenses/GPL-2.0 | |
* | |
* @package Statify/UA Tracking | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly. | |
} | |
/** | |
* Add new column ismobile in statify table on plugin activation | |
* | |
* @since 1.0 | |
*/ | |
function statify_mobile_tracking_activate() { | |
global $wpdb; | |
// Is the column already there? | |
$row = $wpdb->get_results( "SELECT useragent FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '$wpdb->statify' AND column_name = 'useragent'" ); | |
// If not, create the column. | |
if ( empty( $row ) ) { | |
$wpdb->query( "ALTER TABLE '$wpdb->statify' ADD useragent VARCHAR(255);" ); | |
} | |
} | |
register_activation_hook( __FILE__, 'statify_mobile_tracking_activate' ); | |
/** | |
* Add if view is mobile or not on statify__visit_saved action hook. | |
* WARNING: This does not work with JS tracking! | |
* | |
* @since 1.0 | |
* | |
* @param array $data Array of data for visit. | |
* @param string $id ID of the data set. | |
*/ | |
function statify_useragent_tracking_save_visit( $data, $id ) { | |
global $wpdb; | |
$result = $wpdb->update( | |
$wpdb->statify, // Which table is updated? | |
array( | |
'useragent' => $_SERVER['HTTP_USER_AGENT'], // Which data is changed/added? | |
), | |
array( 'ID' => $id ), // Where is the data changed? | |
array( | |
'%s', // Format for added value (string). | |
), | |
array( '%d' ) // Format for where data id. | |
); | |
if (false === $result) { | |
// Nichts aktualisiert. | |
} | |
} | |
add_action( 'statify__visit_saved', 'statify_useragent_tracking_save_visit', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment