At Internetrix, we love the work we do with SilverStripe and Google Analytics and thought why not combine the two? The Internetrix SolutionInternetrix has recently released an open-source module for SilverStripe which allows server-side events to be sent to a Google Analytics property via Google's Measurement Protocol.This essentially allows developers to create a custom DIY event log system that only requires a free Google Analytics account to be created and saves the hassle of having to set up and pay for a standalone logging tool. Developers can then send custom events from a SilverStripe backend directly to Google Analytics. For example, we can track when users login to the CMS, when a purchase has been made, when an Exception occurs, when an email has been sent, code profiling, and timings.The events can then be stored and used as an audit log in Google Analytics which developers and clients alike can access.The Process Step 1To start using the module, simply install it via Composer:composer require internetrix/silverstripe-ga-measurement-protocol
Step 2Once installed, the module needs to be configured so that it knows which Google Analytics property the module should send events to. The steps to do so are in the README file in the module.If you don’t have an existing Google Analytics account, all you have to do is create a free one. Once that’s been done, we can start sending any event we want to Google Analytics.ExampleAs a basic example, let’s send an event whenever a user fails to send an email. In the code, we simply need to do the following.Important - Make sure you don’t send any Personally Identifiable Information to Google Analytics:
// Send an email$email = SilverStripe\Control\Email\Email::create() ->setHTMLTemplate('Email\\MyCustomEmail') ->setFrom($from) ->setTo($to) ->setSubject($subject);$result = $email->send();
// Here's our code to send an event to Google Analytics when an email has failedif (!$result) {$hit = GAHit::create();$hit->setHitType(GAHit::EVENT);$hit->setClientID(false);$hit->setUserAgent();
// Here is the custom event we want to send. Set parameters accordingly to the event you want to send$hit->setEventParameters(Emails, 'Failed to Send', 'Contact Form Submission Email');$hit->setDocumentLocationURL('www.example.com.au/foo');$hit->setNonInteractionHit();
// 3, 2, 1...There goes our event to Google Analytics$hit->sendHit();}
While this is a really basic example, the possibilities of what we can send are really limitless. For more information on how the module works, be sure to check out the README file.To Wrap Things UpYou can try out the new Internetrix Google Analytics Measurement Protocol module for the SilverStripe Framework today by checking it out on GitHub. If you have any questions or need support implementing the module, let us know!