{"id":2230,"date":"2008-09-23T07:26:00","date_gmt":"2008-09-23T07:26:00","guid":{"rendered":"http:\/\/t.motd.kr\/articles\/2230\/inserting-google-analytics-tracking-code-using-shell-script"},"modified":"2022-12-28T01:45:33","modified_gmt":"2022-12-27T16:45:33","slug":"inserting-google-analytics-tracking-code-using-shell-script","status":"publish","type":"post","link":"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/","title":{"rendered":"Inserting Google Analytics Tracking Code using Shell Script"},"content":{"rendered":"\n

In most cases, you should be able to configure your CMS<\/span> or blog template to insert the Google Analytics<\/a> JavaScript tracking code at the end of every page in your web site.<\/p>\n\n\n\n

However, you sometimes need to install the tracker code into static HTML<\/span> pages which are not managed by the CMS<\/span>, If you have a LOT<\/span> of static HTML<\/span> pages, then your fingers are in trouble.<\/p>\n\n\n\n

Here\u2019s the Bash shell script which inserts the Google Analytics tracker code into all static HTML<\/span> pages. Please modify it before you run it, to meet your needs. Never forget to backup the original copy \u2014 it might work incorrectly for you:<\/p>\n\n\n\n

#!\/bin\/bash\n# inject-google-analytics\nTRACKER_ID=\"UA-XXXXX-X\"\nTRACKER_CODE=\"<script type=\\\"text\/javascript\\\">\nvar gaJsHost = ((\\\"https:\\\" == document.location.protocol) ? \\\"https:\/\/ssl.\\\" : \\\"http:\/\/www.\\\");\ndocument.write(unescape(\\\"%3Cscript src='\\\" + gaJsHost + \\\"google-analytics.com\/ga.js' type='text\/javascript'%3E%3C\/script%3E\\\"));\n<\/script>\n<script type=\\\"text\/javascript\\\">\ntry {\nvar pageTracker = _gat._getTracker('$TRACKER_ID');\npageTracker._trackPageview();\n} catch(err) {}<\/script>\"\n\nfind . -name '*.html.new' -delete\nfind . '(' -name '*.html' -and -not -name '*-frame.html' ')' | while read -r TARGET; do\n  if grep -qiP \"(<frameset|$TRACKER_ID)\" \"$TARGET\"; then\n    continue;\n  fi\n\n  if grep -qiF '<\/body>' \"$TARGET\"; then\n    perl -pi -e \"s\/(<\\\/body>)\/\\n${TRACKER_CODE\/\/\/\/\\\/}\\n$1\/i\" < \"$TARGET\" > \"$TARGET.new\"\n  else\n    { cat \"$TARGET\"; echo; echo \"$TRACKER_CODE\"; echo; } > \"$TARGET.new\"\n  fi\n  if ! grep -qF \"$TRACKER_ID\" \"$TARGET.new\"; then\n    echo \"Failed to inject analytics script: $TARGET\"\n    cat \"$TARGET.new\"\n    rm -f \"$TARGET.new\"\n    exit 1\n  fi\n  mv -f \"$TARGET.new\" \"$TARGET\"\ndone<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

In most cases, you should be able to configure your CMS or blog template to insert the Google Analytics JavaScript tracking code at the end of every page in your web site. However, you sometimes need to install the tracker code into static HTML pages which are not managed by the CMS, If you have… Continue reading →<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"yoast_head":"Inserting Google Analytics Tracking Code using Shell Script — T's message of the day<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Inserting Google Analytics Tracking Code using Shell Script — T's message of the day\" \/>\n<meta property=\"og:description\" content=\"In most cases, you should be able to configure your CMS or blog template to insert the Google Analytics JavaScript tracking code at the end of every page in your web site. However, you sometimes need to install the tracker code into static HTML pages which are not managed by the CMS, If you have... Continue reading →\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/\" \/>\n<meta property=\"og:site_name\" content=\"T's message of the day\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/trustin\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/trustin\" \/>\n<meta property=\"article:published_time\" content=\"2008-09-23T07:26:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-27T16:45:33+00:00\" \/>\n<meta name=\"author\" content=\"Trustin Lee\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/www.twitter.com\/trustin\" \/>\n<meta name=\"twitter:site\" content=\"@trustin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Trustin Lee\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/\"},\"author\":{\"name\":\"Trustin Lee\",\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d\"},\"headline\":\"Inserting Google Analytics Tracking Code using Shell Script\",\"datePublished\":\"2008-09-23T07:26:00+00:00\",\"dateModified\":\"2022-12-27T16:45:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/\"},\"wordCount\":117,\"publisher\":{\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/\",\"url\":\"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/\",\"name\":\"Inserting Google Analytics Tracking Code using Shell Script — T's message of the day\",\"isPartOf\":{\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#website\"},\"datePublished\":\"2008-09-23T07:26:00+00:00\",\"dateModified\":\"2022-12-27T16:45:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/vault.motd.kr\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Inserting Google Analytics Tracking Code using Shell Script\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#website\",\"url\":\"https:\/\/vault.motd.kr\/wordpress\/\",\"name\":\"T's message of the day\",\"description\":\"the best is yet to come\",\"publisher\":{\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/vault.motd.kr\/wordpress\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d\",\"name\":\"Trustin Lee\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/vault.motd.kr\/wordpress\/wp-content\/uploads\/2020\/09\/avatar-2019-966px.png\",\"contentUrl\":\"https:\/\/vault.motd.kr\/wordpress\/wp-content\/uploads\/2020\/09\/avatar-2019-966px.png\",\"width\":966,\"height\":966,\"caption\":\"Trustin Lee\"},\"logo\":{\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/t.motd.kr\/\",\"https:\/\/www.facebook.com\/trustin\",\"https:\/\/www.instagram.com\/trustinlee\/\",\"https:\/\/www.linkedin.com\/in\/trustin\",\"https:\/\/twitter.com\/https:\/\/www.twitter.com\/trustin\",\"https:\/\/www.youtube.com\/@trustinlee\"]}]}<\/script>","yoast_head_json":{"title":"Inserting Google Analytics Tracking Code using Shell Script — T's message of the day","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/","og_locale":"en_US","og_type":"article","og_title":"Inserting Google Analytics Tracking Code using Shell Script — T's message of the day","og_description":"In most cases, you should be able to configure your CMS or blog template to insert the Google Analytics JavaScript tracking code at the end of every page in your web site. However, you sometimes need to install the tracker code into static HTML pages which are not managed by the CMS, If you have... Continue reading →","og_url":"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/","og_site_name":"T's message of the day","article_publisher":"https:\/\/www.facebook.com\/trustin","article_author":"https:\/\/www.facebook.com\/trustin","article_published_time":"2008-09-23T07:26:00+00:00","article_modified_time":"2022-12-27T16:45:33+00:00","author":"Trustin Lee","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/www.twitter.com\/trustin","twitter_site":"@trustin","twitter_misc":{"Written by":"Trustin Lee","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/#article","isPartOf":{"@id":"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/"},"author":{"name":"Trustin Lee","@id":"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d"},"headline":"Inserting Google Analytics Tracking Code using Shell Script","datePublished":"2008-09-23T07:26:00+00:00","dateModified":"2022-12-27T16:45:33+00:00","mainEntityOfPage":{"@id":"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/"},"wordCount":117,"publisher":{"@id":"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/","url":"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/","name":"Inserting Google Analytics Tracking Code using Shell Script — T's message of the day","isPartOf":{"@id":"https:\/\/vault.motd.kr\/wordpress\/#website"},"datePublished":"2008-09-23T07:26:00+00:00","dateModified":"2022-12-27T16:45:33+00:00","breadcrumb":{"@id":"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/vault.motd.kr\/wordpress\/posts\/2230\/inserting-google-analytics-tracking-code-using-shell-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vault.motd.kr\/wordpress\/"},{"@type":"ListItem","position":2,"name":"Inserting Google Analytics Tracking Code using Shell Script"}]},{"@type":"WebSite","@id":"https:\/\/vault.motd.kr\/wordpress\/#website","url":"https:\/\/vault.motd.kr\/wordpress\/","name":"T's message of the day","description":"the best is yet to come","publisher":{"@id":"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vault.motd.kr\/wordpress\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d","name":"Trustin Lee","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/vault.motd.kr\/wordpress\/wp-content\/uploads\/2020\/09\/avatar-2019-966px.png","contentUrl":"https:\/\/vault.motd.kr\/wordpress\/wp-content\/uploads\/2020\/09\/avatar-2019-966px.png","width":966,"height":966,"caption":"Trustin Lee"},"logo":{"@id":"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/t.motd.kr\/","https:\/\/www.facebook.com\/trustin","https:\/\/www.instagram.com\/trustinlee\/","https:\/\/www.linkedin.com\/in\/trustin","https:\/\/twitter.com\/https:\/\/www.twitter.com\/trustin","https:\/\/www.youtube.com\/@trustinlee"]}]}},"_links":{"self":[{"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/posts\/2230"}],"collection":[{"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/comments?post=2230"}],"version-history":[{"count":3,"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/posts\/2230\/revisions"}],"predecessor-version":[{"id":5953,"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/posts\/2230\/revisions\/5953"}],"wp:attachment":[{"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/media?parent=2230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/categories?post=2230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/tags?post=2230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}