VMware Modern Apps Community
billrothjr
VMware Employee
VMware Employee

Sending Google Analytics data from PHP to Wavefront

I am posting a short article here since I do not time for a long one.

In short, as part of my job, I need to watch our web site. I want to get all our marketing data into Wavefront so we can see it. So 2 questions come up:

  1. How do I pull data from Google Analytics
  2. How do I send that data to Wavefront in PHP(which I assume would be easy, and was).

I took the code for this from:  https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php

The GITHub repo is here: GitHub - BillRothVMware/wavefront-web-analytics-php

Setup

The code is not pretty. You are warned.

To this this up, you'll need the PHP file and:

1. You'll need to get the composer stuff mentioned above: composer require google/apiclient:^2.0

2. You'll need to get the service credential file. See google web page above for instructions

Deployment

Deployment is a little funky. You have to do the following:

1. Make sure you get app the composer files

2. Move the service*.json to the directory for the php file

3. Move the "vendor" directory to the directory where the php file exists

Sending Data

The basic sending of data is pretty simple. See below.

function sendWavefront($metric, $value, $ts, $tags, $source) {

   

    global $logging;

   

    $address = <MACHINE WITH PROXY>;

    $service_port = 2878; // WF proxy port.

   

    /* Create a TCP/IP socket. */

    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

    if ($socket === false) {

        echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";

        return;

    } else {

        //

    }

    $result = socket_connect($socket, $address, $service_port);

    if ($result === false) {

        echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";

        return;

    } else {

        //

    }

    $str = $metric . " " . $value . " " . $ts . " " . $tags . " source=" . $source . "\n";

   

    if($logging)

        syslog(LOG_INFO,$str);

    socket_write($socket, $str, strlen($str));

    socket_close($socket);

   

}

KTbradley​ and EricNielsen​ FYI. My code still rocks.

------
Bill Roth, VMware
1 Reply
admin
Immortal
Immortal

:smileycheck::smileycheck::smileycheck:

^ The Katie Bradley stamp of approval, this absolutely rocks.