Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Is that coding correct? - A analytics plugin for google

How do i post that php coding here?
It is very simple and very less line

How do i post coding in questions?

Comments

  • hgtonighthgtonight MVP
    edited November 2013

    You can post readable code in a few different ways. You can use backticks `for inline code. You use three tildes (~~~) on their own line to indicate the start and end of a code block.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • maheshmahesh New
    edited November 2013

    plugins/analytics/default.php

    I extened the gdn_plugin
    I got plugin info

      public function Base_AfterBody_Handler(&$Sender)
      {
    echo <<<ANALYTICS
    <!-- Google Analytics -->
    

    <script>

      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    
      ga('create', 'UA-000000-0, 'url');
      ga('send', 'pageview');
    

    </script>

    ANALYTICS;
      }
      public function Setup()
      {
    
      }
    
  • Yes. But is that right way?
    I need to create more plugins

  • @mahesh said:
    Yes. But is that right way?
    I need to create more plugins

    the right way is the way that works correctly, with the least amount of code, the best performance and the necessary amount of flexibility.

    Does this meet those three criteria?

  • Use $Sender instead of &$Sender!
    As far as I know, the leading & was necessary to pass a variable by reference to a function and that is now the default behavior of recent PHP versions.

    You do not need to have the Setup function in your code. It is not wrong there, but it is simply not needed.

  • x00x00 MVP
    edited November 2013

    @mahesh said:
    Yes. But is that right way?
    I need to create more plugins

    This is a valid question.

    Yes it is an acceptable means for this case.

    note you should really declare script like so

    <script type="text/javascript"> rather than <script> although browser are quite forgiving this is more correct.

    One sticker is as it is not included as a resource or module it can't be managed as such.

    However in this case it is acceptable, becuase all that matter is it is included, but in general you wouldn't include javascript like this.

    You might exclude certain parts of the forum from analytics, so you can put the logic for that.

    @hbf said:
    Does this meet those three criteria?

    There is more to it that that, there is also forward/backward compatibility, consistency, transportability/transplantability, standards and conventions, UI concerns, etc, etc.

    grep is your friend.

  • x00x00 MVP
    edited November 2013

    @R_J said:
    Use $Sender instead of &$Sender!
    As far as I know, the leading & was necessary to pass a variable by reference to a function and that is now the default behavior of recent PHP versions.

    You do not need to have the Setup function in your code. It is not wrong there, but it is simply not needed.

    variable can explicitly be passed by reference, except object which are always passed by reference.

    only pass variables by reference if absolutely needed

    PHP manual

    Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only >return references when you have a valid technical reason to do so.

    if you do &Sender in 2.1 it will break. It has always been redundant the whole time. PHP hasn't not passed classes by refernce since it has been a viable language.

    in recent version of php you can do this

    function SomeFunction(&$Value){
    
    }
    
    SomeFunction($Value);
    

    but not this

    function SomeFunction($Value){
    
    }
    
    SomeFunction(&$Value);
    

    grep is your friend.

  • @x00 said:
    There is more to it that that, there is also forward/backward compatibility, consistency, transportability/transplantability, standards and conventions, UI concerns, etc, etc.

    i'd lump everything except standards and conventions into "the needed level of flexibility". As for standards and conventions, while it's important to have them, which ones you choose and how you implement when you are a one man show working on your own project really isnt all that important. if i choose strict hungarian notation for my plugins, it's not really wrong, it's just not necessarily consistent with the framework.

    But yes, I was over-simplifying the situation. Intentionally, not out of ignorance.

Sign In or Register to comment.