Saturday, September 19, 2015

Exclude yourself from Weebly and Google Analytics tracking

I'm helping my sister-in-law with a website to sell items for Orthodox weddings and baptisms. She and her friend have created some beautiful products. Check them out at Dahlia Weddings and Baptisms.

They use Weebly. Lately, I've set up Google Analytics to monitor web traffic. But I get a lot of noise from our own visits to the dahlia website. So I'm trying to filter that out.  This is a challenge. In Google Analytics you can filter by IP Address, but those change often if you don't have a fixed IP (i.e. home users, mobile users).  You can also download a blocker from google, but that won't work on all browser, tablets, or phones.

So I found that the best universal solution involves "cookies" and adding some code to your Weebly site.  I'll take you on a step-by-step journey to get it working.

1.  If you haven't already, create a Google Analytics account and follow google's directions. More help with Weebly here.  Be sure that Google Analytics confirms that your site is connected.

2. Get your Tracking  Id from Google Analytics:  go to /GoogleAnalytics/Admin/PropertySettings [this is my shorthand notation for "go to your google analytics account. Click 'Admin' (at the top). Then click 'Property Settings' (at the top of the middle column)] and copy and paste the id into a text editor.  It has a 'UA' followed by a number.  Copy the whole thing. (I like to copy&paste things into a text editor so that I don't make errors in typing.)

3. Add your Tracking ID to Weebly: go to your Weebly account and click 'edit' on your site in the site list. Click on 'Store', then 'Settings', then 'Advanced' (shorthand is /Weebly/Store/Settings/Advanced). Enter your tracking ID in the input box, but without the 'UA' as it's already there.  So enter the 'XXXXXXXX-X' portion. Click 'Update Changes'.

4. Now we we will add some code that does the disabling when a cookie is detected:
a.  click 'settings' in the top row.  Click 'SEO' in the left column.  (shorthand is /Weebly/Settings/SEO.)
b.  Copy and Paste the code below into the 'Header Code' box:  (Put this in the header code box even though Weebly says below the 'footer code' box to put the Google Analytics tracking code into that box.  I prefer it in the header.  If you already have some google analytics code in one of these boxes, please remove it.
<script>
//put in your own google analytics tracking id below:
var gaProperty = 'UA-XXXXXXXX-X';

// Disable tracking if the opt-out cookie exists.
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
  window[disableStr] = true;
  window['ga-disable-UA-7870337-1'] = true;  //This disables the tracking on Weebly too.
}
</script>
c. Copy and Paste that Google Analytics tracking id you saved (starts with 'UA') in place of the 'UA-XXXXXXXX-X' in the code.  Keep the single quotes ( ' ) around it.  
d. click 'Save'.
5.  Now we will add a special page to your Weebly site to let you opt out of tracking yourself:
a. Go to /Weebly/Pages.  Click '+ Add' button and select 'Standard Page'.  
b. type in a page name. 
Something like 'do not track me'.
c. click 'Hide page in navigation' (We don't want this to be visible to everyone.)
d. select 'no header' for header type.
e. select 'visibility' of 'public'.
f. click 'advanced settings'.
g. copy and paste the following code into the 'Header Code' box:

<script>
//put in your own google analytics tracking id below:
var gaProperty = 'UA-XXXXXXXX-X';
var disableStr = 'ga-disable-' + gaProperty;

// Opt-out function
function gaOptout() {
  document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
  window[disableStr] = true;
  gaOptoutCheck();
}

// Check Opt-out function
function gaOptoutCheck() {
    var name = "ga-disable-"+gaProperty+"=";
    var ca = document.cookie.split(';');
    var found = "false";
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) found = "true";
    }
    if (found == "true") alert("Cookie is properly installed");
    else alert("COOKIE NOT FOUND");
}
</script>
h. once again, Copy and Paste that Google Analytics tracking ID you saved in place of the 'UA-XXXXXXXX-X' in the code. Keep the single quotes ( ' ) around it.
i. click 'Hide this page from search engines'.
We don't want this page to be visible to the search engines.
j. click 'Save & Edit'
6. If you just clicked the 'Save & Edit' from above, you should end up at the 'Build' editor for that page.  If not, you need to go back to /Weebly/Pages/do not track me (or whatever you called the page) and click 'Save & Edit'.  This is the only way to get to the 'Build' editor for this page since we hid it from our navigation (a Weebly limitation).
a. on the left side, click and drag the 'EMBED CODE' element over to the right side where it says 'Drag  Elements Here'.
b. copy the following code:

<a href="javascript:gaOptout()">Click here to opt-out of Google Analytics</a><br><br>
Please visit this page on every computer, laptop, phone, tablet, etc. that you use; and for all browser you use on each of those devices.<br><br>
If you ever remove cookies from browser, you will need to repeat this process for that browser.<br><br><br>
<a href="javascript:gaOptoutCheck()">Click to check if cookie is set</a><br><br>
c. click on the 'Click to set custom HTML' and paste the code there.
7. Click 'PUBLISH' in the upper right of the Weebly screen.  We are basically done.  Now time to test it out.

8. (Optional) If Google Analytics is already tracking your site, lets first check that we see our own clicks.
a. in a new browser tab (Ctrl T), go to /GoogleAnalytics/Reporting/RealTime/Overview.
b. in a new browser tab (Ctrl T), go to your website and click on around the site.
c. switch back to the Google Analytics tab and see if the pages you clicked are listed and your location shows up.
9. Now go to the new page we created and opt out of tracking yourself:
a. in a new tab (Ctrl T), go to the new page by going to 'www.yoursitename.com/do-not-track-me'.  Fill in your site URL for the 'www.yoursitename.com'.  It might be something like 'mybigstore.weebly.com' or 'www.mybigstore.com'.  If you named your page differently than I did, then just use that name at the end with hyphens in place of any spaces in the name.
b. you should see the 'Click here to opt-out of Google Analytics' link we created.  Click on this link.
10. (Optional) If you did step 8 above and saw results in Google Analytics, now go back to your own site and click around a bit.  Then switch back to the Google Analytics tab.  Your page clicks should now be invisible to Google Analytics! 

4 comments: