Google Tag Manager with angular js links - angularjs

I some links in my application which I want to track the click on those links with the help of GTM. Imagine I have a link to download page like this
<a ui-sref="main.download">Download</a>
I create a tag of type 'Universial Google Analytics' for this click named MainDownloadLink.
How should I define a trigger to fire this tag when that link is clicked? Also note that I don't want to use angularitics library. Imagine that I have no control over the source code. just GTM panel.
I define a trigger of type Clicks-Just Link and I am confused for the condition when this event should be fired.
any help is welcomed.

You must make sure that "link validation" is disabled (since GTM does not consider links without href to be valid).
As long as your links are actually named "Download" it probably would be easiest to use the built-in "click text" variable and set a filter "if click text equals download" on your link trigger.
Else you might want to try to access the "main.download" attribute. The built-in click element variable returns the clicked DOM element, so you'd create a custom variable
function() {
return {{Click Element}}.getAttribute('ui.sref');
}
and then test in your trigger if "{{your custom js var}} equals main.download".
Untested, but basically this should work.

Related

Is it possible to make GTM to tag some elements that are dynamically created

I'm a React developer and I barely knows how GTM works as it's not my field.
What I understand is
There are triggers on google tag manager
GTM script will find HTML elements of the triggers by defined css selectors(in my case) and tag them(put GTM attributes) when it's loaded in the page
When an event meeting the condition of a trigger occurs, the trigger will execute defined javascript(in my case)
The problem is that HTML elements possibly don't exist when GTM script is tagging because they can be created by a user interaction. And I want to tag such elements by using GTM's lifecycle method(if exists). Is there any way to do this or at least a workaround?
The best practice I can come up with is something like :
When you render the element you want to track in the React.
Let React push the datalayer about the information of the element like
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: "element_render",
element_id: "#id",
element_type: "type",
any_paramter_you_need: "any_value_you_need",
})
So you can set up the Trigger in you Google Tag Manager and the type is Custom Event
Usually that should work out of the box. GTM does not tag elements directly. Instead, GTM is attached to the document root. If an element is clicked, the event bubbles up to the document root, and there GTM checks if the properties of the event target (the actually element interacted with) meet the conditions defined in the trigger.
So unless React is doing something super crazy with the document, you should be safe. Allowing to track dynamically added elements is pretty much the point of this design.
For visibility triggers, you specifically need to enable this behavior by clicking the checkbox on "observe DOM changes". There is a warning that this might impact page speed.

AngularJs custom directive call hirearchy

I am new to AngularJs. I have written a custom directive in AngularJs. The link for the same can be found here: Link to plunkr. I have added the alerts on loading of the page and on clicking the individual links.
My specific question is why is the alert message "Loaded the custom directive." being triggered after the link is clicked. I was under the impression that The message will be called only on document load and not on every time the links are clicked. Am I doing an mistake or this is the intended behavior.
Please let me know where I am going wrong.
you are changing the page when you click the anchor
<a href="#" class="ops-block">
simply delete the hash.
Is an empty href valid?

Image crop with AngularJS

I want to let the user crop an image, I found this JQuery plugin - http://deepliquid.com/content/Jcrop.html
I tried to use it with Angular-ui's Jquery passthrough option, adding the ui-jq=Jcrop directive to the <img>
The problem is that If I use ng-src to dynamically change the image it doesn't work and nothing is seen. If I change it to src and put a static url I can see the image and Jcrop.
how can I fix that ?
also, how can I listen to Jcrop's callbacks to know what is the user's selection ?
is there a better / simpler way to add image cropping functionality to AngularJS ?
Here is my solution:
I've written a directive that create img element and apply plugin on it. When src is changed, this img is removed and content that was created by plugin is also destroyed and then re-created new img with new src and again applied plugin on it.
Also provided 'selected' callback to be able to get coordinated that were selected (wrapped in $apply so you can modify your scope values in it).
Check my solution at Plunker
I've built a demo using AngularJS and Jcrop here:
Demo: https://coolaj86.github.com/angular-image-crop
On Github: https://github.com/coolaj86/angular-image-crop
You can leverage ui-event to create an event definition object with the keys being the event names and the values being the callbacks. Or you can simply pass these events as options to Jcrop (according to the documentation)
Finally, there is a new update coming to ui-jq that lets you add ui-refresh which is an expression to be watched to re-trigger the plugin.
Theoretically you should be able to do
<img ui-jq="Jcrop"
ui-options="{onSelect:myCallback}"
ui-event="{onChange:'myCallback($event)'}"
ui-refresh="imgSrc"
ng-src="imgSrc" />
Note: this simply re-fires the passthrough again, and doesn't automatically mean this will fix the problem or that the plugin will behave properly when re-initialized
We're still working on a good way to allow you to trigger different events at different times.

Put custom links outside weblinks section

How can I put custom links outside the web links section in a salesforce page?
Or is there a way I can create more than one weblinks section?
If not is there a way I can create a custom field that can call a javascript method ? (my custom links calls a js method that after validation will call a web service to do some work.)
I tried creating a custom field (formula) but I wasnt able to call a js function from there, or put some script.
What I want to do is spread my custom links I have in the weblinks section into different sections on the page.
I have used this method to override link and button actions in the past. You could use the same method for move elements around the page too.
Create a visualforce page and use it to override the standard page for the object you want to modify. The override page needs to use the standardcontroller for that object. You can use an extension controller for adding Apex functionality if necessary.
Use the detail tag to render the guts. Then put in a script tag to select the anchor links you want to move around the page. It will take some inspection with your browsers debugger to find exactly what you are looking for.
<apex:page StandardController="CustomObject__c" extensions="CustomObjectController" >
<apex:detail relatedList="false" inlineEdit="true" id="mydetail" />
<script>
// select your elements you want to move, edit or delete here
</script>
</apex:page>

Salesforce: Launch S-Control in a new window from VisualForce

I'm writing a VisualForce page to replace an old legacy S-Control.
Originally the Custom S-Control was launched from a Custom Button, and it opened in a new browser window (The S-Control is type HTML, and the Custom Button has Behavior: Display in new window)
For certain old records, I want to do a similar thing from VisualForce.
So: Do HTML S-Controls have a URL that I can launch using a link? In which case, how do I figure out the URL?
Or:
Is there a way of embedding 'Custom Buttons' (that is, the buttons defined in "Setup->Customise->Account->Buttons and Links") into VisualForce pages? If so, I can embed the existing button that knows how to open the S-Control
Or:
Can you suggest some other way of doing this? Key features: Open an S-Control in a new window from VisualForce.
Thanks
Got an answer from this forum post courtesy of aballard:
Basically, the !Urlfor() function can be used to get the URL of an S-Control. So, you can use an outputLink like this:
<apex:outputLink value="{!UrlFor($SControl.my_scontrol_name)}"
target="_blank">Show SControl</apex:outputLink>
If you need to pass an object ID to the S-Control, you can add a second parameter that calls a propery on the custom controller, e.g:
<apex:outputLink value="{!UrlFor($SControl.my_scontrol_name, AccountID)}"
target="_blank">Show SControl</apex:outputLink>
... where AccountID is defined on the custom controller as public string getAccountID()
See also this blog post at SalesforceSource for more info on the UrlFor function.

Resources