com.databasesandlife.util.wicket.GoogleAnalyticsAsyncWidget Maven / Gradle / Ivy
Show all versions of java-common Show documentation
package com.databasesandlife.util.wicket;
import org.apache.wicket.Component;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
/**
* Includes Google Analytics tracking into the page.
*
* Which google account is used for the tracking is probably
* deployment-specific. This is therefore passed as a parameter to the
* constructor of this object. It is advised that this parameter be set in the
* web.xml file of the application, to allow it to be different on different
* deployments.
*
* If the account is null or empty, then the analytics tracking code is not
* inserted into the page. (Perhaps some installations require the code, and
* other installations do not.)
*
* @author This source is copyright
* Adrian Smith and
* licensed under the LGPL 3.
* @see Project on GitHub
*/
@SuppressWarnings("serial")
public class GoogleAnalyticsAsyncWidget extends Panel {
public GoogleAnalyticsAsyncWidget(String wicketId, String trackingId) {
this(wicketId, trackingId, true);
}
public GoogleAnalyticsAsyncWidget(String wicketId, String trackingId, boolean useTheOldGoogleAnalyticsApi) {
super(wicketId);
var setAccountJs = "var googleAnalyticsAsyncWidgetGoogleAccount = '" + trackingId + "';";
var setAccount = new Label("setAccount", setAccountJs).setEscapeModelStrings(false);
add(setAccount);
var oldGoogleAnalyticsBlock = new WebMarkupContainer("javascript");
oldGoogleAnalyticsBlock
.setVisible(useTheOldGoogleAnalyticsApi && trackingId != null && !trackingId.isEmpty());
add(oldGoogleAnalyticsBlock);
var newGoogleAnalyticsBolock = new WebMarkupContainer("new-javascript");
newGoogleAnalyticsBolock
.setVisible(!useTheOldGoogleAnalyticsApi && trackingId != null && !trackingId.isEmpty());
add(newGoogleAnalyticsBolock);
}
}