Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Script content getting stripped


Reply

5 Posts   1352 Views

Avatar
steve_nyhof

31 October 2009 at 1:58pm Community Member, 224 Posts

I have been doing quite fine with this. I have setup my LeftAndMain.php file by first adding in the script[scr|... to the end of the valid_elemants

I have also try 'valid_elements' => "*

  • ", star...star

    But SS keeps stripping half of this code for google website optimizer...
    This is what I insert...

    <script type="text/javascript">
    if(typeof(_gat)!='object')document.write('<sc'+'ript src="http'+
    (document.location.protocol=='https:'?'s://ssl':'://www')+
    '.google-analytics.com/ga.js"></sc'+'ript>')</script>
    <script type="text/javascript">
    try {
    var gwoTracker=_gat._getTracker("UA-11120272-2");
    gwoTracker._trackPageview("/2027641438/test");
    }catch(err){}</script>

    SS strips it to this...

    <script type="text/javascript"><!--
    if(typeof(_gat)!='object')document.write('<sc'+'ript src="http'+
    (document.location.protocol=='https:'?'s://ssl':'://www')+
    '.google-analytics.com/ga.js"></sc'+'ript>')
    // --></script>
    <script type="text/javascript"></script>

    Removing the second script - try {...

    Anyone have some ideas? I want this done on the page, not on the template.

    I was wondering if I could add a second custom metadata field, and then a tag $CustomMeta2 right before the /body tag ???

Avatar
dalesaurus

31 October 2009 at 5:09pm (Last edited: 31 October 2009 5:09pm), Community Member, 283 Posts

Your second approach would work, but this seems like the third post I've replied to you saying you should move your JS into .js files then include them in templates instead of finding so many damned ways to hack around and not do it that way.

Seriously, I just did google analytics with js templates and it was a breeze to switch out code tracker codes/HTTPS, etc. If you'd like to know how to do this the easy way, tell me you'll stop this hackery!

Avatar
steve_nyhof

31 October 2009 at 6:47pm (Last edited: 31 October 2009 6:49pm), Community Member, 224 Posts

I promise to stop the hackery! But I just am not a programmer and need to get this project off the ground.

I was able to add a field into my metadata tab for a body script which works great and I'm moving forward. Now I do not need to add the head and body scripts into the editor.

One question I have is how to add spaces to my CustomBodyTags name... Custom Body Tags so it shows up nice on the Metadata tab area. Also, how can I make the field 5 rows high to look like the other Meta tag fields?

The reason I do not want to make templates is because I want to make many split pages, each require a different script code/id.

public static $db = array(
'CustomBodyTags' => 'HTMLText'
);

public static $has_one = array(
"HeaderImage" => "Image"
);

function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Main", new ImageField("HeaderImage"));
$fields->addFieldToTab('Root.Content.Metadata', new TextField('CustomBodyTags'));
return $fields;
}

}

Avatar
steve_nyhof

31 October 2009 at 7:51pm Community Member, 224 Posts

I got this figured out now too...

$fields->addFieldToTab("Root.Content.Metadata", new TextareaField("CustomBodyTags", "Custom Body Tags"));

Added the TextareaField and the , "Custom Body Tags" to get my spaces.

Avatar
dalesaurus

1 November 2009 at 8:03am (Last edited: 1 November 2009 8:05am), Community Member, 283 Posts

Bravo, sir! Now here is how you do it with JS Templates:

1. Create a place to store your codes with your Pages, similar to what you did above, in Page.php

public static $db = array(
   'GoogleTrackingCode' => 'Varchar(255)'
);

function getCMSFields() {
   $fields = parent::getCMSFields();
   $fields->addFieldToTab('Root.Content.Metadata', new TextField('GoogleTrackingCode'));
   return $fields;
}

2. Create a JS Template file, for this example: mysite/templates/javascript/ga.template.js

try { var pageTracker = _gat._getTracker("{$TrackCode}"); pageTracker._trackPageview(); } catch(err) {}

3. User Requirements to put your custom variables in your template, in Page.php

public function init() {
   parent::init();
   if( isset($_SERVER['SSL']) || (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ) {
      Requirements::javascript('https://ssl.google-analytics.com/ga.js');
   } else {
      Requirements::javascript('http://www.google-analytics.com/ga.js');
   }

   $jsData = array('TrackCode' => $this->data()->GoogleTrackingCode );
   Requirements::javascriptTemplate('mysite/templates/javascript/ga.template.js',$jsData);
}

Now on a per page basis you can specify your tracking code. It isn't a 100% solution but it should be a good start to storing only what you need and using templates to consolidate code. Of course what you did is also perfectly OK, but I just want to reinforce the methods that exist as the creators intended.