How to Add Google Analytics to WordPress

Standard

There’s no doubt Google Analytics sets the standard when it comes to gathering information on your site’s visitors.

Analytics can show where they landed on your site, what they read, how long they stayed, what they were searching, the browser they were using, where they live, their age and even their interests.

Adding Google Analytics to your site is easy. So simple, in fact, that it takes less than a minute!

Add Google Analytics to your side in under a minute.

1. Login to your Google Analytics Account

This tutorial assumes you’ve already signed up for Google Analytics. If you haven’t already got one, it only takes a second to sign up, especially if you’ve already got an account with Google. Head to the Google Analytics site and create a new account.

After you’ve signed up, you’ll be given a piece of Javascript code to add to your site.

Tracking code
After signing up for Google Analytics, you’ll be given some code to put in your site.

2. Add the Google Analytics Code to Your WordPress Theme

Copy the tracking code. Go to your WordPress Admin and then Appearance > Editor.

Open your theme’s header.php file and directory underneath the < body > tag, paste the tracking code. Click “Update File”.

Adding code to header
Add the tracking code directly below the < body > tag.

3. Check Analytics is Working On Your Site

Receiving data
Once you’ve insert the tracking code into your theme, your Google Analytics tracking should change.

Go back to your Google Analytics account. Click on “Admin” at the top of the page and under “Property” choose your site and then click “Tracking Info”.

If the Tracking Status is “Receiving Data,” then you’ve successfully added Google Analytics to your site.

You can now click on the “Reporting” tab at the top of the page to see your site’s analytics overview. It may take Google up to 12-24 hours to start providing you with site stats.

Hibernate custom Criteria : check the length of string properties

Standard

There is no in-built function to fetch string properties length in hibernate criteria. However Hibernate also allows the creation of a custom Criterion that can be used in the criteria.

Like,


select * from tbl where length(ENTITY) = 7;

I created a simple Criterion class that is used to check the length of string properties.

 

@SuppressWarnings("serial")
public class LengthExpression implements Criterion {
	private final String propertyName;
	private final int value;

	public LengthExpression(final String propertyName, final int value) {
		this.propertyName = propertyName;
		this.value = value;
	}

	@Override
	public String toSqlString(final Criteria criteria,
			final CriteriaQuery criteriaQuery) throws HibernateException {
		criteriaQuery.getFactory().getDialect();
		final String[] columns = criteriaQuery.getColumnsUsingProjection(
				criteria, this.propertyName);
		final String queryFragment = "length(" + columns[0] + ") = ?";
		return queryFragment;
	}

	@Override
	public TypedValue[] getTypedValues(final Criteria criteria,
			final CriteriaQuery criteriaQuery) throws HibernateException {
		// An ordered pair of a value and its Hibernate type
		return new TypedValue[] { new TypedValue(Hibernate.INTEGER,
				Integer.valueOf(value), EntityMode.POJO) };
	}
}

In the above code:

The class implements the Criterion Interface which exposes two methods.
The toSqlString() method is what generates the actual sql fragment. As we work with prepared statements, “?” ares used. In this case the sql length function is used. The alias used for property in the actual query is obtained using the getColumnsUsingProjection() method.
The getTypedValues() method tells Hibernate about the data Type to be used in the query. In this case the parameter will be set using the preparedStatement.setInt() method.

To test the code, I used it in a criteria example:

public static void testCustomCriterion() {
		final Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(Entity.class);
		criteria.add(new LengthExpression("name", 7));
		List entities = criteria.list();
		System.out.println(entities);
	}

The result of the execution in hibernate is below:

    /* criteria query */
    select
        this_.ID as ID0_0_,
        this_.NAME as NAME0_0
    from
        ENTITY this_
    where
        length(this_.NAME) = ?

MySQL database backup restore

Standard

What is the quickest way to backup & restore mysql database?

First open command prompt(cmd) and get folder location from where we want to generate backup file or want to read file for restore.

For Local backup:

Backup: c:> mysqldump -u [MySQL User] -p db_name > file_name.dmp;
Restore: c:> mysql -u [MySQL User] -p db_name < file_name.dmp;

For Remote backup:

Backup: c:> mysqldump -u [MySQL User] -h [Your server's IP address] -p db_name > file_name.dmp;
Restore: c:> mysql -u [MySQL User] -h [Your server's IP address] -p db_name < file_name.dmp;

Then, it will prompt for password and done.

Note: Backup file will not support in all mysql version to restore. so first verify version. For that kind of problem you can find different different solution as per two version of mysql. like,

Backup:(—routines)

c:> mysqldump --routines -u root -p db_name > file_name.dmp;

For Restore it will same as above noted.

Change MySQL auto increment value

Standard

First Check what is current increment value :

SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND TABLE_NAME = 'TableName';

Now, Set auto increment value:

ALTER TABLE tbl AUTO_INCREMENT = 5;