Description
Tag initialisation
SmartTag JavaScript file (to generate in Tag Composer and host on your site) needs to be initialised in the head in order to trigger page hits.
1 2 3 4 5 6 7 8 9 10 11 |
<head> <script src="./js/smarttag.js"></script> </head> <body> <script> var tag = new ATInternet.Tracker.Tag(); </script> </body> |
Use page title as page name
To rely on this option, be sure that the current page already has a title in the first lines of your page.
1 2 3 4 |
<html> <title>My Beautiful Page Title</title> |
Then call the document.title in the page name tag parameter after the tracker initialisation just like this.
1 2 3 4 5 6 7 8 9 |
<script> var tag = new ATInternet.Tracker.Tag(); tag.page.set({ name: document.title, }); tag.dispatch(); </script> |
You should get a hit with &p=My%20Beautiful%20Page%20Title displayed as “My Beautiful Page Title” in the analysis.
Or use page URL as page name
With this option your page URL is parsed to become page name and chapters. First thing is to define the variables storing the various versions of your URL labels (files, folders…) before the tag initialisation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<script> //URL Parsing var parser = document.createElement('a'); parser.href = document.URL; parser.protocol; // => "http:" parser.hostname; // => "example.com" parser.port; // => "3000" parser.pathname; // => "/pathname/" parser.search; // => "?search=test" parser.hash; // => "#hash" parser.host; // => "example.com:3000" //Chapters for URL ending by extensions (http://www.internet.com/test/1234/toto/SmartTag/page.html ) var pathNameA1 = parser.pathname.replace(/\//g, '::'); var pathNameA2 = pathNameA1.substring(2); // var pathNameA = '' + pathNameA2 + ''; var pageFileA1 = pathNameA.match(/(::)(?!.*\b\1\b).+/g); var pageFileA2 = '' + pageFileA1 + ''; var pageNoSlash = pageFileA2.substring(2); //Chapters for URL ending by a slash (http://www.internet.com/test/blog/) var pathNameB1 = parser.pathname; var pathNameB1 = pathNameB1.substring(0, pathNameB1.lastIndexOf("/")); var pathNameB2 = pathNameB1.replace(/\//g, '::'); var pathNameB = pathNameB2.substring(2); if (pathNameB.match(/::/g) == null) { var countPathNameB = 0; }else{ var countPathNameB = pathNameB.match(/::/g).length; } var pageSlashA1 = pathNameB.match(/(::)(?!.*\b\1\b).+/g); var pageSlash = '' + pageSlashA1 + ''.substring(2); |
Then to adapt to each URL structure you need to add these conditions based on the URL loading the page. Please edit the URLs on second line to match your home page URLs, and send “home” as page label.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
//URL detection implying chapter variables choices if(document.URL == "http://www.internet.com/" || document.URL == "http://www.internet.com/index.html" || document.URL == "http://www.my-domain-is-so-cool.com/" || document.URL == "http://www.my-domain-is-so-cool.com/index.html" || document.URL == "http://www.this-one-is-cool-too.com/" || document.URL == "http://www.this-one-is-cool-too.com/index.html"){ var page = "home"; }else if (document.URL.slice(-1) == '/' && countPathNameB == 1) { //"http://www.internet.com/test/blog/" var myRegexp0nA2 = /([a-zA-Z0-9-_]*)::/g; var regexOnA2 = myRegexp0nA2.exec(pathNameB); var chapterAT1 = regexOnA2[1]; var pageSlashCase = pageSlash.substring(2); var chapterA = chapterAT1.replace('::', '/'); var chapterB = ''; var chapterC = ''; var page = pageSlashCase; }else if (document.URL.slice(-1) == '/' && countPathNameB == 2) { //"http://www.internet.com/test/blog/example/" var myRegexp0nA3 = /([a-zA-Z0-9-_]*)::([a-zA-Z0-9-_]*)::/g; var regexOnA3 = myRegexp0nA3.exec(pathNameB); var chapterAT1 = regexOnA3[1]; var chapterAT2 = regexOnA3[2]; var pageSlashCase = pageSlash.substring(2); var chapterA = chapterAT1.replace('::', '/'); var chapterB = chapterAT2.replace('::', '/'); var chapterC = ''; var page = pageSlashCase; }else if (document.URL.slice(-1) == '/' && countPathNameB >= 3) { //"http://www.internet.com/test/blog/2016/09/11/bardenas/" var myRegexp0nA4 = /([a-zA-Z0-9-_]*)::?([a-zA-Z0-9-_]*)::?([a-zA-Z0-9-_]*.+)::/g; var regexOnA4 = myRegexp0nA4.exec(pathNameB); var chapterAT1 = regexOnA4[1]; var chapterAT2 = regexOnA4[2]; var chapterAT3 = regexOnA4[3]; var pageSlashCase = pageSlash.substring(2); var chapterA = chapterAT1.replace('::', '/'); var chapterB = chapterAT2.replace('::', '/'); var chapterC = chapterAT3.replace(/::/g, '/'); var page = pageSlashCase; }else if (document.URL.slice(-1) != '/' && pathNameA.match(/::/g) == null) { //"http://www.internet.com/testurl.html" var chapterA = ''; var chapterB = ''; var chapterC = ''; var page = parser.pathname.substring(1); }else if (document.URL.slice(-1) != '/' && pathNameA.match(/::/g).length == 1) { //"http://www.internet.com/images/testurl.html" var myRegexp0nB2 = /([a-zA-Z0-9-_]*)::/g; var regexOnB2 = myRegexp0nB2.exec(pathNameA); var chapterAT1 = regexOnB2[1]; var chapterA = chapterAT1.replace('::', '/'); var chapterB = ''; var chapterC = ''; var page = pageNoSlash; }else if (document.URL.slice(-1) != '/' && pathNameA.match(/::/g).length == 2) { //"http://www.internet.com/images/test/testurl.html" var myRegexp0nB3 = /([a-zA-Z0-9-_]*)::([a-zA-Z0-9-_]*)::/g; var regexOnB3 = myRegexp0nB3.exec(pathNameA); var chapterAT1 = regexOnB3[1]; var chapterAT2 = regexOnB3[2]; var chapterA = chapterAT1.replace('::', '/'); var chapterB = chapterAT2.replace('::', '/'); var chapterC = ''; var page = pageNoSlash; }else if (document.URL.slice(-1) != '/' && pathNameA.match(/::/g).length >= 3) { //"http://www.internet.com/test/AT/GTMTag/SmartTag/testurl.html" var myRegexp0nB4 = /([a-zA-Z0-9-_]*)::?([a-zA-Z0-9-_]*)::?([a-zA-Z0-9-_]*.+)::/g; var regexOnB4 = myRegexp0nB4.exec(pathNameA); var chapterAT1 = regexOnB4[1]; var chapterAT2 = regexOnB4[2]; var chapterAT3 = regexOnB4[3]; var chapterA = chapterAT1.replace('::', '/'); var chapterB = chapterAT2.replace('::', '/'); var chapterC = chapterAT3.replace('::', '/'); var page = pageNoSlash; } |
Eventually, set your page tagging right after the tag initialisation just like this.
1 2 3 4 5 6 7 8 9 10 11 |
var tag = new ATInternet.Tracker.Tag(); tag.page.set({ name: page, chapter1: chapterA, chapter2: chapterB, chapter3: chapterC, }); tag.dispatch(); </script> |
Reviews
There are no reviews yet.