Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi klihelp,

    The analytics.js example at google.com shows only two parameters being used.

    ga('create', 'UA-XXXX-Y');

    When the full block of js code is copied out of the official Google Analytics administration panel, it does have a third parameter.

    ga('create', 'UA-XXXX-Y', 'DOMAIN.TLD');

    This would explain the plugin upgrade instructions to no longer use just the UA code but to have everyone update their ‘WordPress’ -> ‘Settings’ -> ‘Google Universal Analytics’ option to be the full js code; no longer just the UA code itself.

    The main benefit of the earlier versions of this plugin was to allow us to only enter the UA code in the options and the plugin took care of the rest.

    Upgrading “Google Universal Analytics” plugin from v1-1 to v1-2 is NOT backwards compatible and effectively brakes the web site.

    So here is some code to make it backward compatible.

    The following code has been packaged into a WordPress plugin installable ZIP:

    function google_universal_analytics() {
      $web_property_id = get_option( 'web_property_id' );
    
      // Is the option just the UA id?
      $web_property_id_p1 = strtoupper( trim( $web_property_id ) );
      $web_property_id_p2 = '';
      if ( strpos( $web_property_id_p1, 'UA-' ) === 0 ) {
    
        // Does the option have a second parameter? (Comma separator)
        $web_property_id_p2_pos = strpos( $web_property_id, ',');
        if ( $web_property_id_p2_pos !== false ) {
    
          // Isolate the first parameter.
          $web_property_id_p1_pos = strpos( $web_property_id_p1, ',');
          if ( $web_property_id_p1_pos !== false ) {
            $web_property_id_p1 = substr( $web_property_id, 0, $web_property_id_p1_pos );
            if ( $web_property_id_p1 !== false ) {
              $web_property_id_p1 = strtoupper( trim( $web_property_id_p1 ) );
            } else {
              $web_property_id_p1 = '';
            }
          }
    
          // Isolate the second parameter.
          $web_property_id_p2_pos += 1;
          $web_property_id_p2 = substr( $web_property_id, $web_property_id_p2_pos );
          if ( $web_property_id_p2 !== false ) {
            $web_property_id_p2 = strtolower( trim( $web_property_id_p2 ) );
          } else {
            $web_property_id_p2 = '';
          }
        }
    
        // Build up a whitespace trimmed javascript that is the GA tracking script.
        // Include the option. One or two values.
        $web_property_id_js = '<script>';
        $web_property_id_js .= '(function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){';
        $web_property_id_js .= '(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),';
        $web_property_id_js .= 'm=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)';
        $web_property_id_js .= '})(window,document,\'script\',\'//www.google-analytics.com/analytics.js\',\'ga\');';
        $web_property_id_js .= 'ga(\'create\',\'' . $web_property_id_p1 . '\'';
        if ( strlen( $web_property_id_p2 ) > 1 ) {
          $web_property_id_js .= ',\'' . $web_property_id_p2 . '\'';
        }
        $web_property_id_js .= ');ga(\'send\',\'pageview\');';
        $web_property_id_js .= '</script>';
    
        $web_property_id = $web_property_id_js;
      }
      if ( strpos( $web_property_id, 'UA-' ) !== false ) {
        echo $web_property_id;
      }
    }

    Download: wordpress_plugin_google-universal-analytics_v1-2-0-2.zip

    Note: This ZIP is not officially endorsed by anyone. Use at your own discretion after a code review.

    Kind regards,
    John.

    Plugin Author Audrius Dobilinskas

    (@audriusd-1)

    Closing the topic. New version is available ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Keep it simple – ask only for the tracking code number’ is closed to new replies.