2014-06-12

Accessing a portal page metadata using Javascript.

The WebSphere Portal has got a client based interface towards many of it's functions, this interface is called the mashup client interface and comes from the mashup server product which has been incorporated into the WebSphere Portal product.

Here is a small example of how to read the pagemetaData parameters from a page using javascript:
        var nm = com.ibm.mashups.enabler.navigation.Factory.getNavigationModel();
        var selectedNode = nm.find(ibmCfg.portalConfig.currentPageOID).start();
        // fetch the currentpage metadata only
       selectedNode.getmetaData("metdataparametername");
       // fetch all aggregated metadata values
        metadatavalue = aggregatedPageMetadata["metadataparameter"];
Here is an example of how to store new metadata values:

    var nm = com.ibm.mashups.enabler.navigation.Factory.getNavigationModel();
    var selectedNode = nm.find(ibmCfg.portalConfig.currentPageOID).start();
    selectedNode.setMetaData("cds.css." + type ,url);    
    nm.commit().start(); // Sync save.
var asyncSave = nm.commit(); 
asyncSave.setFinishedCallback(commited);
asyncSave.setOperationCallback(commitoper);
asyncSave.start();

// finished callback function
function commited(result, status) {
    // handle overall status
    if (status == 200  status == 201) {
        alert("Successfully committed navigation model.");
    } else {
        alert("Failed to commit navigation model;
HTTP status code '" + status + "'.");
    }
}
// operation callback function
function commitoper(result, operation, status) {
 if (operation == asyncSave.OPERATION_SAVE){
}else if (operation == asyncSave.OPERATION_MODIFY){
}else if (operation == asyncSave.OPERATION_DELETE){
}

}

2014-01-07

Simple adding of Google analytics code on a portal dynamic spot item:

<%-- Google Analytics include file for the page --%>
<portal-logic:pageMetaData varname="pageMetaData">
    <c:set var="gacode" value="${pageMetaData['ga.code']}" />

<c:if test="${not empty gacode}">
<script type="text/javascript">
(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');

ga('create', '${apsgacode}', '<company>.com');
ga('require', 'linkid', 'linkid.js');// page tracking enable
gaFriendly = document.location.indexOf("/!ut");
gaLoc = document.location.href;
if (gaFriendly != -1){
gaLoc = document.location.href.substring(0,gaLoc);
}
ga('set', 'location', gaLoc);
${pageMetaData['ga.option1']}
${pageMetaData['ga.option2']}
${pageMetaData['ga.option3']}
ga('send', 'pageview');
</script>
</c:if>
</portal-logic:pageMetaData>

This code will add a simple support for google analytics on a portal theme page, I added this to the head.jsp file for now but can be included anywhere.
Since we use the page meta data i.e parameters we will be able to use the same theme on many virtual portals.