Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8476679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:14:47+00:00 2026-06-10T18:14:47+00:00

The Google Analytics tracking code looks like this: (function() { code })(); What’s the

  • 0

The Google Analytics tracking code looks like this:

(function() {
code
  })();

What’s the technique they are using with those brackets – (stuff)() – called? What does it do?

I put the Google Analytics code before the closing head tag on my page and then put an if statement around it like this (I include a Jquery cookie plugin further up):

<script type="application/javascript">
if ($.cookie('allowcookies') == 'yes') {
analytics code
}
</script>

It didn’t run until I used the same technique around my code:

(function() {if ($.cookie('allowcookies') == 'yes') {

analytics code
}
})();

Why did it not run before I did that? Why did it run after?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:14:49+00:00Added an answer on June 10, 2026 at 6:14 pm
    (function() {
       /* code */
    }()); 
    

    It’s commonly known as «self executed anonymous function (¹)» (o «immediate function invocation») and its main use is to avoid the creation of variables into the global (or in the outer) scope.

    It’s also used as shortcut when you want to create a function to execute just once, without the need to first define the function with its own identifier and then soon make the function call.

    It may be eventually used inside a scope and then it may create a closure if the outer context (or other references) are binded through parameters passing, e.g.

    /* outer scope */  
    (function(outerscope) {
    
       element.onsomeevent = function() {
           /* do something with outerscope */
       };
    
    }(this));
    

    Another practical use I make with this expression is when I need to create a function to be soon executed inside a constructor function when it is called with new keyword (instead of an explicit call to some init method).


    (¹) — as stated on book “Mantainable Javascript” by Nicholas Zakas (O’Reilly, ISBN 978-1-449-32768-2) page 44, the suggested expression is (function() {}()), with nested parens (even if (function() {})() will work anyway)

    […]To make it obvious that immediate function invocation is taking place, put paretheses around the function[…]

    See also Immediate function invocation syntax

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Async Tracking code in Google Analytics looks like this: var _gaq = _gaq
The Google Analytics setup instructions state: This tracking code snippet should be included in
I am using google analytics event tracking to track 2 Facebook canvas application. (they
This is my first time using Google Analytics Ecommerce Tracking to get data for
Take this scenario. I have a Google Analytics tracking code, and I only want
I am using Google Analytics Asynchronous tracking code and trying to track clicks on
Has anybody tried this before? How can i amend the google analytics tracking code
New Google Analytics code looks like one below: <script type=text/javascript> var _gaq = _gaq
Currently using Google Analytics as a supplement to our paid tracking software, but neither
We're trying to figure out how common web tracking software works, like Google Analytics.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.