Adtoniq For Java

Because Adtoniq requires your web server and Adtoniq’s servers to communicate directly, you will need to add some code to your Java-based web server in order to enable this communication. Adtoniq for Java is a library you can add to your Java-based web server to enable Adtoniq’s features for your website. The high level steps to use Adtoniq for Java are:

  1. Register to get an Adtoniq account and your API key
  2. Download the Adtoniq for Java code and build it
  3. Add one line to the <head> section of all pages
  4. Add one line to the <body> section of all pages
  5. Understand how to view your ad block analytics

These steps are explained in greater detail below.

Register to get an Adtoniq account

Prior to using Adtoniq, you must register your web server to get an API Key which you will use in your code. The API key is bound to the fully qualified domain name of your website. To register your web server, go to the Adtoniq registration page and fill out the required information.

Download Adtoniq for Java and build it

Download the source files for Adtoniq for Java from git, which is provided as a completely working Eclipse / maven project you can build and deploy, and then modify from there or merge with your existing project. There are several key files:

  1. Adtoniq.java is all the code required to connect your website to Adtoniq services. Add this file to you your java project. We provide this as source code only at this point and may release a jar file and publish in the maven repository as well if customers ask.
  2. AdtoniqLauncher.java shows you how to initialize Adtoniq with your API key and fully qualified domain name. You’ll need to customize this file with the API key you get during registration, and the fqdn of your web site, and add this code to your project as well.
  3. index.jsp is a sample page that shows you how to modify your web pages to add Adtoniq. You’ll need to add a line to the header and to the body of all pages on your site.
  4. analytics.jsp shows you how to view your ad block analytics.

Adtoniq for Java has a dependency on Apache’s commons-io library:

 <dependency>
   <groupId>commons-io</groupId>
   <artifactId>commons-io</artifactId>
   <version>2.5</version>
 </dependency>

Add one line to the <head> section of all pages

At the top of the <head> section of all the JSP pages on your website, make a call to the getHeadCode() function. This will insert Adtoniq’s JavaScript inline into your page. The code below shows the simplest approach for adding the <head> code to a page. You may also have a mechanism to add something to all pages at once (such as a commonly included header template), in which case you would place this code in your header template.

<head>
 <%=AdtoniqLauncher.adtoniq.getHeadCode(request)%>
...

Add one line to the <body> section of all pages

At the top of the <body> section of all the JSP pages on your website, make a call to the getBodyCode() function. This will insert an invisible 1×1 iframe on your website that performs ad block detection. As soon as the ad block detection phase is completed, the iframe is automatically deleted to save on resources. The code below shows the simplest approach for adding the body code to a page. You may also have a mechanism to add something to all pages at once (such as a commonly included body template), in which case you would place this code in your body template.

<body>
 <%=AdtoniqLauncher.adtoniq.getBodyCode()%>
...

Viewing your ad block analytics

Visit the Adtoniq Analytics page to view your analytics. You will be required to sign in with the email address and password you used when you registered your website. Alternatively, you can easily build your own in-house ad block analytics page by taking a look at the analytics.jsp page in the Adtoniq project; this provides the complete code to host your own Java-based analytics page.

Most of the action happens inside an iframe, where Adtoniq will render your analytics. The only slightly tricky part is securely sending your API key to Adtoniq. See the source code in analytics.jsp for comments and details.