Wednesday, November 7, 2012

Google Tag Manager tutorial.

As the name says, it’s a tag manager offered by Google for free. Here is one basically tutorial show you what it can do, what kind of benefit it can bring to your table.

Basic Idea, you just Insert one google tag manager script to your website, then From the google tag manager console, you can push to deploy different other scripts. like anlaytics tagging, even a customized html , or javascript, img tag.

So first sign up for a account, create one container, get the script you need to inject to your website, try insert the script after <body> tag. if you insert it to blogger, parse the special tags, like & to &amp;

image

Then time to create some tags and test to see the tags on your website. 
  we create one tag to inject the firstaccess cookie to record the first time user hit the web site,

image

for the rules, we applied to all pages.

that’s for tag creating process, now we need to publish this tags.
Click the create version button,
image
it will create one new version with all the current tags,
in each version , you can click save and publish. or SaveandPreview.

savepreview give us change to make sure tags executed. so we click the save and preview mode.
you may see the msg telling you you are in the preview and debug mode. yes, we are!

Now go you your website,
a window will popoup showing you which tag get executed, for me, the inject cookie one.
image

once confirmed ok, exit the preview mode and publish it.
now, go to my web page, you can see we have the firstaccess cookie is there, we injected successfully.
here is the code for your reference,

<script>
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

if( ! getCookie('firstaccess'))
{
setCookie('firstaccess',new Date(),100);
}

</script>

No comments:

 
Locations of visitors to this page