Tuesday, April 23, 2013

Google Analytics Panel: Wicket

Generates JavaScript for Google analytics tracking code using wicket panel:

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

 
public class GoogleAnalyticsPanel extends Panel {


    private IModel<String> accountNameModel;

    public GoogleAnalyticsPanel(String id) {

      super(id);

      accountNameModel=new Model<String>("your account number");

      Label label=new Label("accountName", accountNameModel);

      label.setRenderBodyOnly(true);

      label.setEnabled(true);

       add(label);

    }

    @Override

    protected void onDetach() {

        super.onDetach();

        if (accountNameModel != null) {

            accountNameModel.detach();

        }

    }
 
    /**

     * @param accountName

     */

    public void setAccountName(String accountName) {

        this.accountNameModel = new Model<String>(accountName);

    }

    /**

     * @return

     */

    public IModel<String> getAccountNameModel() {

        return accountNameModel;

    }


    /**

     * @param accountNameModel

     */

    public void setAccountNameModel(IModel<String> accountNameModel) {

        this.accountNameModel = accountNameModel;

    }


}

GoogleAnalyticsPanel.html

 

<?xml version="1.0" encoding="UTF-8"?>

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">

<body>

<wicket:panel>

       var _gaq = _gaq || [];

        _gaq.push(['_setAccount', '<wicket:container wicket:id="accountName"></wicket:container>']);

        _gaq.push(['_trackPageview']);

 

        (function() {

          var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;

          ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

        })();

</wicket:panel>

</body>

</html>

In webpage add the Google analytics panel :

                GoogleAnalyticsPanel googleAnalyticsPanel=new GoogleAnalyticsPanel("googleAnalyticsScript");

add(googleAnalyticsPanel);

In Html page add the panel in java script section :

<script type="text/javascript" wicket:id="googleAnalyticsScript">

</script>

No comments:

Post a Comment