Skip to content

Commit

Permalink
CCGoogleAnalytics
Browse files Browse the repository at this point in the history
Added a tiny Dynamic component to out google analytics code

Signed-off-by: Amedeo Mantica <amedeo.mantica@insigno.it>
  • Loading branch information
Amedeo Mantica committed Sep 3, 2012
1 parent 59ffdba commit 9bd5327
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Frameworks/Ajax/ERCoolComponents/Components/CCGoogleAnalytics.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?><wodefinitions>
<wo class="CCGoogleMap.java" wocomponentcontent="false">

<binding name="uaid"/>









<validation message="'uaid' is a required binding.">
<unbound name="uaid"/>
</validation>
</wo>
</wodefinitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package er.coolcomponents;

import com.webobjects.appserver.WOAssociation;
import com.webobjects.appserver.WOContext;
import com.webobjects.appserver.WOElement;
import com.webobjects.appserver.WOResponse;
import com.webobjects.foundation.NSDictionary;

import er.extensions.components.ERXDynamicElement;

/**
* <p>
* CCGoogle analytics is a very simple compoment (dynamic element) that insert the google analytics code</p>
* <p>
*
* @binding uaid you goocle analytics id: UA-XXXXXX
*
* @author amedeomantica
*/

public class CCGoogleAnalytics extends ERXDynamicElement {

public CCGoogleAnalytics(String name,
NSDictionary<String, WOAssociation> associations,
WOElement children) {
super(name, associations, children);
// TODO Auto-generated constructor stub
}


@Override
public void appendToResponse(WOResponse aResponse, WOContext aContext) {
super.appendToResponse(aResponse, aContext);

String googleId = stringValueForBinding("uaid", aContext.component());

if(googleId!=null) {

aResponse.appendContentString("<script type=\"text/javascript\">");

aResponse.appendContentString("var _gaq = _gaq || [];");
aResponse.appendContentString("_gaq.push(['_setAccount', '" + googleId + "']);");
aResponse.appendContentString("_gaq.push(['_trackPageview']);");
aResponse.appendContentString("(function() {");
aResponse.appendContentString("var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;");
aResponse.appendContentString("ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';");
aResponse.appendContentString("var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);");
aResponse.appendContentString("})();");

aResponse.appendContentString("</script>");

}

}


}

0 comments on commit 9bd5327

Please sign in to comment.