{"id":45183,"date":"2017-01-20T12:22:56","date_gmt":"2017-01-20T06:52:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=45183"},"modified":"2017-01-30T14:45:02","modified_gmt":"2017-01-30T09:15:02","slug":"how-to-access-google-analytics-dimensions-in-aem","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-access-google-analytics-dimensions-in-aem\/","title":{"rendered":"How to Access Google Analytics Dimensions in AEM?"},"content":{"rendered":"<p><span style=\"font-weight: 400\">I was working on a project <\/span><span style=\"font-weight: 400\"><a href=\"https:\/\/github.com\/IntelliGrape\/aem-ga\">CONAG<\/a>\u00a0<\/span><span style=\"font-weight: 400\">where I had to create a mapping page for <a href=\"http:\/\/www.tothenew.com\/blog\/google-analytics-mapping-console-in-aem\/\">Google Analytics<\/a> (similar to what Adobe Analytics provide for AEM) in which I needed all the dimensions of Google Analytics so that I could map these dimensions with <a title=\"AEM development\" href=\"http:\/\/www.tothenew.com\/wcm\/cq-aem-development-consulting\">AEM<\/a>\u00a0variables.<\/span><\/p>\n<p><span style=\"font-weight: 400\">To get these dimensions of an account, we need to have Analytics object for which we need to have GoogleCredential object.<\/span><\/p>\n<p><span style=\"font-weight: 400\">In this blog, I will be discussing the approach to get GoogleCredential object in AEM as rest of the part is straightforward and Google has already given an <\/span><a href=\"https:\/\/developers.google.com\/analytics\/devguides\/reporting\/core\/v3\/quickstart\/service-java\"><span style=\"font-weight: 400\">example <\/span><\/a><span style=\"font-weight: 400\">for the same.<\/span><\/p>\n<p><strong><span style=\"font-weight: 400\">To get GoogleCredential object, we need to set private key of service account.<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">There are two ways of doing this; even Google provides two different methods for this:<\/span><\/strong><\/p>\n<ul>\n<li>setServiceAccountPrivateKeyFromP12File (java.io.File file)<br \/>\n<span style=\"font-weight: 400\">In this method, we need to get File object of .p12 file (provided by Google). G<\/span><span style=\"font-weight: 400\">etting a java.io.File object is very tedious in AEM as it does not interact with the file system directly.<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">So, first\u00a0we need to get inputstream of the resource containing .p12 file data from content hierarchy (in AEM, everything is content) and write it into a temporary file. But converting the .p12 file into a\u00a0temporary file would give the exception because it is encrypted.<\/span><strong><strong><span style=\"font-weight: 400\"><strong><br \/>\n<\/strong>So we have to use setServiceAccountPrivateKey which takes java.security.PrivateKey as a parameter.<\/span><\/strong><\/strong>&nbsp;<\/li>\n<li><strong><span style=\"font-weight: 400\">setServiceAccountPrivateKey(java.security.PrivateKey privateKey)<br \/>\n<\/span><\/strong><span style=\"font-weight: 400\">To get the java.security.PrivateKey from .p12 file, we need to get the inputstream of file as<\/span><\/p>\n<p>[java]Resource fileResource = resourceResolver.getResource(filePath);<br \/>\nfinal Node node = fileResource.adaptTo(Node.class);<br \/>\nfinal InputStream is = node.getProperty(&quot;jcr:data&quot;).getBinary().getStream();[\/java]<\/p>\n<p>Now when we have the inputstream of the file, we need to get the java.security.KeyStore as<\/p>\n<p>[java]final KeyStore keyStore = KeyStore.getInstance(keyStoreType, keystoreProvider);[\/java]<\/p>\n<p>Load the KeyStore object as<\/p>\n<p>[java]keyStore.load(is, filePassword.toCharArray());[\/java]<\/p>\n<p>Finally get the PrivateKey as:<\/p>\n<p>[java]PrivateKey privateKey = (PrivateKey) keyStore.getKey(keyAliasName,filePassword.toCharArray());[\/java]<\/p>\n<p>Get the HttpTransport object:<\/p>\n<p>[java]final HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();[\/java]<\/p>\n<p>Get GsonFactory:<\/p>\n<p>[java]GsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();[\/java]<\/p>\n<p>Now, we can get the GoogleCredential object:<\/p>\n<p>[java]final GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(JSON_FACTORY).setServiceAccountId(serviceAccountEmail).setServiceAccountPrivateKey(serviceAccountPrivateKey).setServiceAccountScopes(AnalyticsScopes.all()).build();[\/java]<\/p>\n<p>And get the Analytics object from GoogleCredential:<\/p>\n<p>[java]analytics = new Analytics.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(appName).build();[\/java]<\/p>\n<p>Now, we have Google Analytics java object and we can perform any operation like create dimension, get dimension, create a\u00a0report and many other operations which this API provides.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I was working on a project CONAG\u00a0where I had to create a mapping page for Google Analytics (similar to what Adobe Analytics provide for AEM) in which I needed all the dimensions of Google Analytics so that I could map these dimensions with AEM\u00a0variables. To get these dimensions of an account, we need to have [&hellip;]<\/p>\n","protected":false},"author":569,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[446,1],"tags":[4847,4051,4267,171,4844],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/45183"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/569"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=45183"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/45183\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=45183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=45183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=45183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}