The CreatorCon Call for Content is officially open! Get started here.

Google Analytics on Helsinki Service Portal

brian_
Tera Guru

Has anyone put Google Analytics on the Service Portal? I put it as a javascript include at the theme level. This seems to work, but only for the initial page the user hits or any refreshes. Since navigating around the portal typically doesn't involve reloading a new page, it would seem GA doesn't get the hit that the URL has changed.

I tried putting the tracking code into a widget in the header on all pages, but that didn't seem to work any better.

Any ideas? Thanks

1 ACCEPTED SOLUTION

Did you create a UI script, or did you go through Service Portal > Themes > your theme > JS Includes? This is where the script will need to be. <edit: I re-read your post and saw you did that>


Also, remove the <script> tags (lines 1 & 10) from your script include. The yellow dot errors can be ignored.



That implementation should work great. I did end up combining that JS Include into the header widget as well for simplicity's sake. Here is a screenshot.



find_real_file.png


(if you choose to do it this way, you'll have to edit it in the Service Portal Configuration > Widget Editor. Editing it directly from Widgets will not let you save as the script editor barks at that red dot error, but that is code directly from Google so I left it alone)





And is working very well.



find_real_file.png


View solution in original post

38 REPLIES 38

Hi Brian,

I am trying to setup the GA with our SN instance. I have created an GA account and got the tracking ID. I have created the UI script as show above. Went to Service Portal -> Theme -> Selected an existing Theme and added the JS Include in the related list. 

I dont see any changes happening in the GA Page. Please let me know if I am missing anything here. 

 

UI Script:

function ($rootScope, $scope, snRecordWatcher, spUtil, $location, $uibModal, cabrillo, $timeout, $window){
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

gs('create', 'UA-Code xxxx, 'auto');

//browser load page view
ga('send', {
hitType: 'pageview',
title: $window.document.title,
location: $location.absUrl()
});
//angular navigate page view
$rootScope.$on('$locationChangeSuccess', function(event, toState, toParams){
ga('send', {
hitType: 'pageview',
title: $window.document.title,
location: $location.absUrl()
});
});
}

The script you have posted here actually goes in the widget if you go that route (see the post marked as correct answer). It will be the entire client controller (plus of course any of your existing code). This method does not require a UI script to be created.

When adding this code into the widget client controller, getting error msg at line # 3.find_real_file.png

That is expected. I mentioned that right below the screenshot posted. You can save and ignore the error.

Deactivated the UI script which was created earlier. I have added the above code to one of the widget client controller but I am not seeing anything reflecting in the GA page. Not sure what I am missing. 

find_real_file.png